Problems with XMLHttpRequest in Chrome

2

After this update of Google Chrome for version 53, I am facing problems in the Ajax (jQuery), $ .http (AngularJS) and XMLHttpRequest (Javascript) calls with the following message:

XMLHttpRequest cannot load *. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '*' is therefore not allowed access. The response had HTTP status code 400.

In similar call of another service, with the same characteristics, I received the following message:

XMLHttpRequest cannot load *. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '*' is therefore not allowed access. The response had HTTP status code 405.

An example call is as follows:

var data = JSON.stringify(false);

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("PUT", "*");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("accept", "application/json");

xhr.send(data);

I also can not run in Firefox and Internet Explorer, but before it worked in Chrome, when I make the calls in the Postman or Backend method they are executed correctly.

I'm facing this problem because I'm implementing one of the api's on a landing page with no backend, so I need to use only Javascript call or jQuery page.

Can anyone help me?

    
asked by anonymous 14.09.2016 / 21:46

1 answer

4

That:

 xhr.open("PUT", "*");

It does not make sense, the second parameter in open must be a relative URL or path, * does not look like a "valid" URL.

See this part of the error message:

  

The response had HTTP status code 405.

Code 405 indicates not allowed method, which refers to the use of PUT , GET , etc, but in your case it is probably the URL you used as an asterisk * .

Probably what you want is something like:

 xhr.open("PUT", "/foo/bar");

The problem with No 'Access-Control-Allow-Origin' header is present on the requested resource. has already been spoken several times on the site:

Search: link

The url you are trying to access is external or a sub-domain, to allow this type of request (from different domains).

    
14.09.2016 / 22:55