标签:style blog http io ar color os 使用 sp
有种方式可以通过JSONP方式来请求
这里具体介绍如何通过修改配置文件来实体AJAX跨域请求WEBSERVICE
WEBSERVICE的类声名
/// <summary> /// MobileService 的摘要说明 /// </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 TestService : System.Web.Services.WebService { }
WEBCONFIG的修改
开启允许POST GET请求
<add name="HttpGet"/>
<add name="HttpPost"/>
开启允许跨域请求
<add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
<add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/>
<!--Value="*"不限制域名 -->
<add name="Access-Control-Allow-Origin" value="http://domain1.com, http://domain2.com" />
<configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <webServices> <protocols> <add name="HttpGet"/> <add name="HttpPost"/> </protocols> </webServices> </system.web> <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/> <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/> <!--Value="*"不限制域名 --> <add name="Access-Control-Allow-Origin" value="http://domain1.com, http://domain2.com" /> </customHeaders> </httpProtocol> <modules> <add name="MyHttpModule" type="WebServiceDemo.MyHttpModule"/> </modules> </system.webServer> </configuration>
AJAX示例代码
$.ajax({
type: "POST",
url: "http://10.10.0.1:8888/XXXXX.asmx/XXXXX",
dataType: "JSON",
contentType: "application/json",
data: { Data1: "HELLO", Data2: "WORD" },
success: function (msg) {
alert(msg);
}
});
标签:style blog http io ar color os 使用 sp
原文地址:http://www.cnblogs.com/enya1999/p/4134027.html