通过“http://localhost:9000/”访问 XMLHttpRequest来自原点“http://127.0.0.1:8000”已被 CORS 政策阻止

发布于 2025-01-21 02:58:17 字数 951 浏览 4 评论 0原文

当前URL:http://127.0.0.1:8000/

当我尝试将请求发送到http://127.0.0.1:9000/时,它提供,它提供CORS错误。但是http://127.0.0.1:9000/当我尝试在浏览器中分别致电时,正常工作。它在单独的Laravel应用程序中正常工作。但是,当它在Elecron-JS应用程序内部使用时,它提供了CORS错误。这样做的原因是什么?我该如何解决这个问题?

代码:

axios.get('http://127.0.0.1:9000', { transformRequest: [(data, headers) => {
   delete headers.common['X-Requested-With'];
   return data 
}] })
.then((response) => {
   console.log(response);
})

控制台:

“在这里输入图像描述”

网络:

​> 17.2.0

电子包装器:15.4.0

laravel:9.5.1

Current url: http://127.0.0.1:8000/

When I trying to send request to http://127.0.0.1:9000/, it provides CORS error. But http://127.0.0.1:9000/ works properly when I trying to call separately in browser. It works properly in separate laravel application. But when it used inside of elecron-js application, it provides CORS error. What is the reason for this? How can I solve this issue?

Code:

axios.get('http://127.0.0.1:9000', { transformRequest: [(data, headers) => {
   delete headers.common['X-Requested-With'];
   return data 
}] })
.then((response) => {
   console.log(response);
})

Console:

enter image description here

Network:

enter image description here

vue: 3.2.31

axios: 0.25.0

electron: 17.2.0

electron-packager: 15.4.0

laravel: 9.5.1

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

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

发布评论

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

评论(1

梦醒时光 2025-01-28 02:58:17

错误的原因是您正在从不是来自原点URL的服务器中请求数据。

您可以在请求标题“网络”选项卡中观察,一个名为 Origin 的标题。这是您的请求的来源。当原点不匹配所请求的服务器时,您会遇到CORS错误。解决此问题的方法是告诉服务器接受不仅http://127.0.0.1.1:9000的原始标头。您的错误消息指出:

No 'Access-Control-Allow-Origin' header is present on the requested resource

access-control-allow-origin控制它将接受的交叉原点请求标头。您需要设置服务器上的access-control-allow-origin标题,以接受http://127.0.0.1.1:8000的来源。

在Node Express Server上,您这样做:

const express = require('express');
const app = express();

const cors = require('cors');

app.use(cors({ origin: 'http://127.0.0.1:8000'})

The reason for your error is that you're requesting data from a server that is not from the origin URL.

You can observe in the Request Headers section in the Network tab, a header called Origin. This is where your request comes from. When the Origin doesn't match the server being requested you get a CORS error. The way to fix this is to tell the server to accept Origin headers that are not only http://127.0.0.1:9000. Your error message states:

No 'Access-Control-Allow-Origin' header is present on the requested resource

Access-Control-Allow-Origin controls which Cross Origin request headers it will accept. You need to set the Access-Control-Allow-Origin header on the server to accept an Origin of http://127.0.0.1:8000.

On Node Express server you do it like this:

const express = require('express');
const app = express();

const cors = require('cors');

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