0

I'm attempting to do an API post but I get the following error, 405 (Method Not Allowed) Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1' is therefore not allowed access.

this is my code:

var data = null;

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

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

xhr.open("POST", "url + parameters");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Basic: username:pasword");
xhr.setRequestHeader("Accept", "application/json, text/plain, */*'");
xhr.setRequestHeader("Access-Control-Allow-Origin", "Http://127.0.0.1");
xhr.setRequestHeader("Access-Control-Allow-Headers", "Content-Type");
xhr.setRequestHeader("Access-Control-Allow-Methods", "POST,GET");

xhr.setRequestHeader("Cache-Control", "no-cache");


xhr.send(data);

Please if anyone could assist. Thank you

1 Answer 1

0

Try this in the .htaccess of the root folder for the destination site :

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

hope this work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.