通过几天的研究效果,如果在vs2010工具上通过webservice还是比较简单的,毕竟是一个项目。

如果您想通过HTML5 做出来的移动APP去访问c#做出来的webservice,那么就没那么简单了,应为不是一个项目,而且部署到外网服务器上以后数据跨域访问。

自己琢磨了两三天,还搞了一台腾讯云服务器来测试,测试没问题,具体操作,直接看代码。

HBuilder 前端html代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title></title>
<script src="js/mui.min.js"></script>
<link href="css/mui.min.css" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="css/index.css"/>
<script type="text/javascript" src="js/jquery-2.1.0.js" ></script>
<script type="text/javascript" src="js/index.js" ></script>
<script type="text/javascript" charset="utf-8">
mui.init();
</script>
</head>
<body>
<header class="mui-bar mui-bar-nav">
<h1 class="mui-title">测试 Ajax 调用云端WebServices</h1>
</header>
<div class="mui-content">
<div id="">
<button type="button" class="mui-btn mui-btn-blue mui-btn-outlined" onclick="dbOnclick1()">Ajax WebServices按钮</button>
</div> <div id="">
<div class="mui-input-row">
<label>name</label>
<input type="text" id="txt-name" placeholder="请输入您的姓名">
</div> <button type="button" class="mui-btn mui-btn-blue mui-btn-outlined" onclick="dbOnclick2()">带参数Ajax WebServices按钮</button>
</div> <div id="divRes"> </div>
<p><br>按钮1,直接获取webservices接口的返回值</p>
<p>按钮2,传输用户输入的内容后,获取webservices接口的返回值</p>
<button type="button" class="mui-btn mui-btn-blue mui-btn-block" onclick="dbOnclick3()">按钮</button>
</div>
<nav class="mui-bar mui-bar-tab">
<a class="mui-tab-item mui-active">
<span class="mui-icon mui-icon-home"></span>
<span class="mui-tab-label">首页</span>
</a>
<a class="mui-tab-item">
<span class="mui-icon mui-icon-phone"></span>
<span class="mui-tab-label">电话</span>
</a>
<a class="mui-tab-item">
<span class="mui-icon mui-icon-email"></span>
<span class="mui-tab-label">邮件</span>
</a>
<a class="mui-tab-item">
<span class="mui-icon mui-icon-gear"></span>
<span class="mui-tab-label">设置</span>
</a>
</nav> </body>
</html>

HBuilder 前端js代码

/// <reference path="jquery-2.1.0.js" />
//function ajaxOn() {
var urlStr1="http://118.89.27.204:8080/WebService1.asmx/HelloWorld";
var urlStr2="http://118.89.27.204:8080/WebService1.asmx/HelloWorldName";
//
// function dbOnclick1(){       jQuery.support.cors = true; //IE10以下
$.ajax({
type: "post",
url: urlStr1,
dataType: 'xml',
//data: { inputStr: 'everyone' },
success: function (data) {
document.getElementById("divRes").innerText=data.lastChild.textContent;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
alert('error:' + errorThrown);
}
});
}
function dbOnclick2(){       jQuery.support.cors = true; //IE10以下
$.ajax({
type: "post",
url: urlStr2,
dataType: 'xml',
data: { name: $("#txt-name").val() },
success: function (data) {
document.getElementById("divRes").innerText=data.lastChild.textContent;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
alert('error:' + errorThrown);
}
});
}

C# webservices代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services; namespace WebApplication2
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
//注意这句,跨域的关键
Context.Response.AddHeader("Access-Control-Allow-Origin", "*");
return "Hello World \r\n 这是一个不带参数的webservices方法";
}
[WebMethod]
public string HelloWorldName(string name)
{
//注意这句,跨域的关键
Context.Response.AddHeader("Access-Control-Allow-Origin", "*");
return name + " Hello World \r\n 这是一个带参数的webservices方法";
}
}
}

如果还是不能获取到webservices服务的数据可以尝试修改配置文件“Web.config”

<?xml version="1.0" encoding="utf-8"?>

<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off"/>
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
</protocols>
</webServices>
</system.web> </configuration>

手机测试图片

          

其他条码知识 请访问:http://www.ybtiaoma.com ,本文仅供参考,请勿转载,谢谢

  

html5 app开发实例 Ajax跨域访问C# webservices服务的更多相关文章

  1. Hbuilder编辑App时,ajax跨域访问失败问题

    今天试着用Hbuilder写app的前段显示页面,在第一步时就被打住了,ajax异步调用服务器的登录接口时,报错, 显示这样的错误 XMLHttpRequest cannot loadhttp://w ...

  2. 浅析JSONP-解决Ajax跨域访问问题

    浅析JSONP-解决Ajax跨域访问问题 很久没有写随笔了,总是感觉没时间,其实时间就是...废话少说,前几天,工作上有一新需求,需要前端web页面异步调用后台的Webservice方法返回信息.实现 ...

  3. ajax跨域访问的解决方案

    今天的工作中要访问摄像机内部的一个web站点,这就涉及到jquery的ajax跨域访问的问题.我使用的是jquery1.7的版本,下面总结如下: 问题一:一开始用IE调试,总是返回No Transpo ...

  4. Ajax跨域访问解决办法

    方法1. jsonp实现ajax跨域访问示例 jsp代码: <body> <input type="button" onclick="testJsonp ...

  5. Web Api 2(Cors)Ajax跨域访问

    支持Ajax跨域访问ASP.NET Web Api 2(Cors)的简单示例教程演示   随着深入使用ASP.NET Web Api,我们可能会在项目中考虑将前端的业务分得更细.比如前端项目使用Ang ...

  6. ajax 跨域访问的解决方案

    ajax 跨域访问的解决方案 一.什么是跨域: 1.什么样的请求属于跨域: 域名,端口有任何一个不相同都属于跨域: 二.跨域的常用几种解决方案: 1.jsonp: 2.iframe: 3.webcon ...

  7. 关于JQuery Ajax 跨域 访问.net WebService

    关于这个 jQuery Ajax跨域访问 WebService 前天整了好几个小时没整明白 今天再看一下 结果突然就顿悟了 1.建一个空webApplication --添加--新建项--web服务( ...

  8. JS Ajax跨域访问

    js ajax跨域访问报"No 'Access-Control-Allow-Origin' header is present on the requested resource 如果请求的 ...

  9. ajax跨域访问http服务--jsonp

    在前面一篇文章<Spring Cloud 前后端分离后引起的跨域访问解决方案>里我们提到使用ajax跨域请求其他应用的http服务,使用的是后台增加注解@CrossOrigin或者增加Co ...

随机推荐

  1. Python版的数据库查询构造器、ORM及动态迁移数据表。

    Orator Orator提供一个简单和方便的数据库数据处理库. 它的灵感来源于PHP的Laravel框架,借助其思想实现了python版的查询构造器和ORM. 这是完整的文档:http://orat ...

  2. C# -- 继承规则

    例子1--C#继承的常见问题: using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  3. 教大家一个看电视局免广告的方法--由UWP想到的

    将近一年(10个月)来一直在学习.NET技术,这其中包括C#.WPF.WCF和ASP.NET MVC,目前学习即将结束. 本人在学习WPF的过程中,也了解到有UWP这门技术,UWP技术目前来说主要是应 ...

  4. Spring MVC 参数的绑定方法

    在Spring MVC中,常见的应用场景就是给请求的Url绑定参数.本篇就介绍两种最最基本的绑定参数的方式: 基于@RequestParam 这种方法一般用于在URL后使用?添加参数,比如: @Req ...

  5. Python Day25

    Django之ModelForm组件 ModelForm a. class Meta: model, # 对应Model的 fields=None, # 字段 exclude=None, # 排除字段 ...

  6. vue中v-bind绑定样式

    近来发现v-bind绑定样式的两个好玩的栗子 可以直接绑定到一个样式对象,让模板更清晰: <div id="app"> <div v-bind:style=&qu ...

  7. Base64Utils

    package com.yundaex.common.crypto.base64; import java.io.ByteArrayInputStream; import java.io.ByteAr ...

  8. 【SQL Server 优化性能的几个方面】(转)

    转自:http://blog.csdn.net/feixianxxx/article/details/5524819     SQL Server 优化性能的几个方面 (一).数据库的设计 可以参看最 ...

  9. Microsoft JDBC Driver 使用 getParameterMetaData 会报错?

    不知道为何使用 Microsoft JDBC Driver for SQL Server 驱动时,sql语句不带参数没有问题,但是如果带参数且使用 getParameterMetaData 就会提示某 ...

  10. 【Linux】linux下vi命令大全

    进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件,并将光标置于最后 ...