[聚合文章] C# ajax跨域

.Net 1900-01-01 20 阅读
  • webconfig的<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>
    • 访问的ajax,改一下contentType,dataType, data处理一下确保传字符串
    var jsonStr = {'userID':1};  $.ajax({    type: "post", //要用post方式        url: "http://url/ajax/mOperate.asmx/getMemberInfo",        contentType: "application/x-www-form-urlencoded; charset=utf-8",        data: toStr(jsonStr),        dataType: "text",        success: function (data) {            console.log(data)        },        error: function (XMLHttpRequest, textStatus, errorThrown) {            console.log(XMLHttpRequest);            console.log(textStatus);            console.log(errorThrown);            console.log("error");        }        });    function toStr(obj) {        var str = [];        for (var p in obj) {            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));        }        return str.join("&");    }

    结束,这样就跨域可以了,比较简单方便

    注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版权等事宜,请你联系站长进行处理。