用于http://的XMLHttpRequest需要跨源资源共享(CORS)和预检仅在IE中发生

Not*_*ing 15 javascript ajax

我是一名前端开发人员.我只在客户端进行编码,所以我不确定错误是否存在.我正在搜索CORS,但仍然不知道我的问题是什么.

我正在尝试将数据发布到REST.

$.ajax({
     url        : urlPost,
     type       : "POST",
     data       : JSON.stringify(obj),
     dataType   : "json",
     contentType: "application/json",

     success: function(res){
         console.log(JSON.stringify(res));
     },

     error: function(res){
         console.log("Bad thing happend! " + res.statusText);
     }
});
Run Code Online (Sandbox Code Playgroud)

web服务的标题显示在firefox的firebug中:

在此输入图像描述

它适用于我使用的所有浏览器,除了在IE 10中,我有两个错误:

  1. SEC7118:XMLHttpRequest用于http://mysite/project/wl.svc/AddWL/所需的跨源资源共享(CORS).

  2. SEC7119:XMLHttpRequest用于http://mysite/project/wl.svc/AddWL/所需的CORS预检.

moo*_*oga 1

我认为你有CORS,所以我建议你看看cors-anywhere

我已经使用过它,它可以正常工作,将您的请求 url 附加到https://cors-anywhere.herokuapp.com/,它是一个代理服务器来请求所选的 URL

var cors_api_url = 'https://cors-anywhere.herokuapp.com/' + urlPost;

$.ajax({
     url        : cors_api_url,
     type       : "POST",
     data       : JSON.stringify(obj),
     dataType   : "json",
     contentType: "application/json",

     success: function(res){
         console.log(JSON.stringify(res));
     },

     error: function(res){
         console.log("Bad thing happend! " + res.statusText);
     }
});
Run Code Online (Sandbox Code Playgroud)