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>
上一篇: PHP高并发和大流量的解决方案
下一篇: python 虚拟环境安装与卸载