-1

I tried to post some data on a certain URL using fetch API and XHR but both of them didn't work and response with 401 status, and when I am testing the post request on postman it work normally.

const rawl = fetch("URL GOES HERE", {
  method: 'POST',
  mode: 'no-cors',
  credentials: "same-origin",
  data: {
    "name": "SOME INPUT VALUES HERE",
    "input": {}
  },
  headers: {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "Authorization": "TOKEN GOES HERE"
  },
});

rawl.then((data) => {
  console.log(data);
})

this with fetch api and this one with xhr

var xmlhttp;
if (window.XMLHttpRequest) {
  xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    document.getElementById("demo").innerHTML =
      this.responseText;
  }
};
xmlhttp.open("POST", "URL GOES HERE", true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.setRequestHeader("Authorization", "TOKEN GOES HERE");
xmlhttp.send({
  "name": "INPUT VALUE GOES HERE",
  "input": {}
});

This return with error: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 401.

but on postman it normally goes with same headers and values!!!

user7637745
  • 965
  • 2
  • 14
  • 27

2 Answers2

0

Well it is hard to say without any information how to reproduce your error. Maybe in Postman you have turned on other token in the Autorization tab or provided another header in the Headers tab.

Maybe take a look at File->Settings->Proxy or/and General where you have some options like "Send Postman Token header".

Karol Samborski
  • 2,757
  • 1
  • 11
  • 18
0

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

It is a cors related error, the preflight request is made by the browser before the actual request to check if your domain has the rights to access the resource.

Postman doesn't do the preflight request that's why you get the response.

You have to modify your backend and allow your origin adding this header Access-Control-Allow-Origin to your specific domain.

No 'Access-Control-Allow-Origin' - Node / Apache Port Issue

Karim
  • 8,454
  • 3
  • 25
  • 33