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 '' 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) {

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://");
xhr.setRequestHeader("Access-Control-Allow-Headers", "Content-Type");
xhr.setRequestHeader("Access-Control-Allow-Methods", "POST,GET");

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


Please if anyone could assist. Thank you


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

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

hope this work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.