jquery ajax跨域请求webservice webconfig配置

时间:2023-03-09 16:23:59
jquery ajax跨域请求webservice webconfig配置
<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"/>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<modules>
<add name="MyHttpModule" type="WebServiceDemo.MyHttpModule"/>
</modules>
</system.webServer>
</configuration>

如果想选定的网站可能跨域访问,修改配置如下

<add name="Access-Control-Allow-Origin" value="http://domain1.com, http://domain2.com" />

JS代码:

function call2() {
$.support.cors = true;
$.ajax({
url: "http://localhost:21179/WebService1.asmx/Test",
type: "POST",
dataType: "json",
contentType: 'application/json', data: "{'user':{'Name':'aaa','Sex':'男','ID':1}}", success: function (d) {
alert("success! ");
},
error: function (e) {
}
});
}

用的时候要注意,这样用可能有风险,具体什么风险还不清楚!!!

有一篇文章可以作为参考

http://www.coderssource.com/?tag=/Access-Control-Allow-Origin

转载:http://blog.csdn.net/liyifei21/article/details/17509735