欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

webApi前端ajax调用后端返回{"readyState":0,"status":0,"statusText":"error"}解决方案

程序员文章站 2023-09-28 19:35:32
var url = data.url, params = data.params, try_times = data.try_times , async = data.sync == 'false' ? false : true; $.ajax({ url: url, type: "POST", t ......
 var url = data.url,
            params = data.params,
            try_times = data.try_times ,
            async = data.sync == 'false' ? false : true;      

        $.ajax({
            url: url,
            type: "post",
            timeout: 60000,
            async: async,
            data: json.stringify(params),
            datatype: "text",
            success: function (data) {
                alert("aaaa+bbb");                
            }, error: function (error) {//增加访问出错信息返回
                alert("出错了" + json.stringify(error));               
            }, complete: function (xmlhttprequest, status) {
} });

前端报错为:{"readystate":0,"status":0,"statustext":"error"},这个问题主要是由于跨域引起的

解决方案如下:

在请求的服务端的web.config中添加如下节点:

<system.webserver>   
    <httpprotocol>
      <customheaders>
        <add name="access-control-allow-origin" value="*" />
        <add name="access-control-allow-headers" value="content-type" />
        <add name="access-control-allow-methods" value="get, post, put, delete, options" />
      </customheaders>
    </httpprotocol>
  </system.webserver>