最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access to XMLHttpRequest at '...' from origin 'http:...' has been blocked by CORS p

programmeradmin8浏览0评论

So my problem is:

Whenever I try to make an XMLHttpRequest to my HTTP server from my web page (same machine, different port), I get blocked with this error:

Access to XMLHttpRequest at 'localhost:8081' from origin 'http://localhost:8080' has been blocked by CORS policy:
Cross-origin requests are only supported for protocol schemes: HTTP, data, chrome, chrome-extension, brave, chrome-untrusted, HTTPS.

Why does this happen?

Sample client (Browser):

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    // Use data...
};
xhttp.open("GET", "localhost:8081", true);
xhttp.send();

Sample server (NodeJS, HTTP library):

const server = http.createServer((req, res) => {
  if(req.url != '/') return res.end('404');
  res.writeHead(200, {
    'Content-Type': 'text/html'
  })

  const json = {
    // Data...
  }

  res.write(JSON.stringify(json))
  res.end()
});

server.listen(8081)

Solved, thanks to Quentin in the ments :D

So my problem is:

Whenever I try to make an XMLHttpRequest to my HTTP server from my web page (same machine, different port), I get blocked with this error:

Access to XMLHttpRequest at 'localhost:8081' from origin 'http://localhost:8080' has been blocked by CORS policy:
Cross-origin requests are only supported for protocol schemes: HTTP, data, chrome, chrome-extension, brave, chrome-untrusted, HTTPS.

Why does this happen?

Sample client (Browser):

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    // Use data...
};
xhttp.open("GET", "localhost:8081", true);
xhttp.send();

Sample server (NodeJS, HTTP library):

const server = http.createServer((req, res) => {
  if(req.url != '/') return res.end('404');
  res.writeHead(200, {
    'Content-Type': 'text/html'
  })

  const json = {
    // Data...
  }

  res.write(JSON.stringify(json))
  res.end()
});

server.listen(8081)

Solved, thanks to Quentin in the ments :D

Share Improve this question edited Jan 11, 2022 at 10:43 IkeVoodoo asked Jan 11, 2022 at 10:37 IkeVoodooIkeVoodoo 1471 gold badge3 silver badges9 bronze badges 4
  • 2 Typo: You forgot the http:// bit of the URL. – Quentin Commented Jan 11, 2022 at 10:41
  • 1 Of course, fixing that will give you a more regular CORS error, but you'd done nothing to support it in the server anyway. – Quentin Commented Jan 11, 2022 at 10:41
  • 1 see stackoverflow./a/35553666/19068 – Quentin Commented Jan 11, 2022 at 10:41
  • 1 Unrelated to the issue at hand, but 'Content-Type': 'text/html' and res.write(JSON.stringify(json)) really doesn't make sense. JSON is not HTML. – Quentin Commented Jan 11, 2022 at 10:42
Add a ment  | 

3 Answers 3

Reset to default 6

The answer is here.

It's a problem with the privacy of your browser and there are two ways that you could fix it:

  1. Allowing requests from everywhere on your browser
  2. Put controllers inside the header access control information.

The entire answer is on the link above

I was facing this issue: Access to XMLHttpRequest at "Node.js server URL" from origin "React app URL" has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I resolved this issue by adding below lines of code in my index.js(main server file):

app.use((req, res, next) => {
  res.header(
    "Access-Control-Allow-Origin",
    "React app URL"
  );
  res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
  res.header("Access-Control-Allow-Headers", "Content-Type");
  next();
});

when you start working with a full stack application. but basically, all that it means is that our front-end and back-end are running on different origins(ports).and therefore they (back-end/front-end) are not allowed to talk each other .. Order to fix this error, it just go into your front-end ./root open ./root folder up the package.json file and you can add to another key:value to

"proxy":"http://localhost:8080/"

just adding proxy in pakage.json file

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论