Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest for http:// required Cross Origin Resource Sharing (CORS) and preflight only happen in IE

I am a front-end developer. I'm coding only with client side so I don't know for sure about the error exist. I've searching about CORS, but still no idea what did my problem course.

I'm trying to post data to 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);
     }
});

Headers of web service show in firebug of firedfox :

enter image description here

It is working for all the browser that I used, except in IE 10, I got two errors:

  1. SEC7118: XMLHttpRequest for http://mysite/project/wl.svc/AddWL/ required Cross Origin Resource Sharing (CORS).

  2. SEC7119: XMLHttpRequest for http://mysite/project/wl.svc/AddWL/ required CORS preflight.

like image 312
Nothing Avatar asked Nov 23 '13 07:11

Nothing


People also ask

Does CORS only work in browsers?

An HTTP client other than a browser won't use either the same origin policy or CORS. Requests made from these other HTTP clients don't have an origin. Unless the Postman desktop app emulates a browser it will be able to make requests to any URL.


1 Answers

I think you have a CORS, so I recommend you to take a look at cors-anywhere

I have used it and it works fine all what you need to append your request url to https://cors-anywhere.herokuapp.com/ which it is a Proxy server to request the chosen 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);
     }
});
like image 97
mooga Avatar answered Oct 04 '22 21:10

mooga