enyo 给了我一个“Access-Control-Allow-Origin 不允许的”。并且不会从 Nodejs 服务器加载内容

发布于 2024-12-26 14:12:35 字数 524 浏览 1 评论 0原文

我制作了简单的 hello world NODEJS 服务器。 我有一个在 chrome 中运行的 enyo Web 服务,它试图访问位于 http://localhost:3000 的 NODEJS

服务器调用onSuccess方法,没有加载数据,consule显示以下错误

XMLHttpRequest cannot load http://localhost:3000/. Origin http://localhost:81 is not allowed by Access-Control-Allow-Origin.

我在浏览器中测试了nodejs服务器,它工作正常。

我尝试在 Chrome 中设置 --disable-web-security 标志,但没有成功。

有人知道如何解决这个问题吗?如果 NOD.js 在另一台服务器上运行,它会工作吗?这种安全感太让人困惑了。 特德

I made the simple hello world NODEJS Server.
I have a enyo web service running in chrome that is trying to access the NODEJS server at http://localhost:3000

When is calls the onSuccess method, no data is loaded and the consule shows the following error

XMLHttpRequest cannot load http://localhost:3000/. Origin http://localhost:81 is not allowed by Access-Control-Allow-Origin.

I tested the nodejs server in the browser, it worked fine.

I tried to set the --disable-web-security, flag in chrome, it did not work.

Does anybody know how to fix this problem? If NOD.js is running on another server, would it work? This security is so confusing.
Ted

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初吻给了烟 2025-01-02 14:12:35

出于安全原因,浏览器限制脚本可以通过 XMLHttpRequest 发出的请求。

您的请求仅在以下 2 种情况下才会成功:

  1. 脚本加载的 URI 的来源与执行脚本的页面的来源相同(localhost:81 和 localhost:3000 是同一主机但来源不同) );
  2. 或者,如果您的浏览器支持,则所请求页面的服务器包含一个 Access-Control-Allow-Origin 标头,该标头显式授权从相关源提供的页面(或从所有源提供的页面)向其发出 XMLHttpRequest。

尝试将 Accesss-Control-Allow-Origin 标头添加到节点代码中生成响应的任何内容,在某些代码中添加标头,如下所示:

response.writeHead(200, {
                         'Content-Type': 'text/plain',
                         'Access-Control-Allow-Origin' : 'http://localhost:81'

                         //allow anything by replacing the above with
                         //'Access-Control-Allow-Origin' : '*'

});

For security reasons, browsers limit the requests that a script may make via XMLHttpRequest.

Your requests will only succeed under the following 2 cases:

  1. The origin of the URI that your script loads is the same as the origin of the page on which the script is executing (localhost:81 and localhost:3000 are the same host but different origins);
  2. or, if your browser supports it, the server of the page being requested includes an Access-Control-Allow-Origin header which explicitly authorizes pages served from the origin in question (or pages served from all origins) to make XMLHttpRequests to it.

Try adding the Accesss-Control-Allow-Origin header to whatever is generating the response in your node code, adding a header in some code that looks like this:

response.writeHead(200, {
                         'Content-Type': 'text/plain',
                         'Access-Control-Allow-Origin' : 'http://localhost:81'

                         //allow anything by replacing the above with
                         //'Access-Control-Allow-Origin' : '*'

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文