好记性不如烂笔头。

web.config配置webservice jquery ajax跨域请求

Web.config文件配置:

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.0" />     
    </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> 
  </system.webServer>
</configuration>

 

JS代码:

function call2() {
            $.support.cors = true;
            $.ajax({
                url: "http://www.mayb.cn/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) {
                }
            });
        }