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

 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>
posted @ 2020-04-22 20:21  额头有王的喵  阅读(74)  评论(0编辑  收藏