通过“http://localhost:9000/”访问 XMLHttpRequest来自原点“http://127.0.0.1:8000”已被 CORS 政策阻止
当前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:
Network:
vue: 3.2.31
axios: 0.25.0
electron: 17.2.0
electron-packager: 15.4.0
laravel: 9.5.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误的原因是您正在从不是来自原点URL的服务器中请求数据。
您可以在请求标题“网络”选项卡中观察,一个名为 Origin 的标题。这是您的请求的来源。当原点不匹配所请求的服务器时,您会遇到CORS错误。解决此问题的方法是告诉服务器接受不仅
http://127.0.0.1.1:9000
的原始标头。您的错误消息指出:access-control-allow-origin
控制它将接受的交叉原点请求标头。您需要设置服务器上的access-control-allow-origin
标题,以接受http://127.0.0.1.1:8000
的来源。在Node Express Server上,您这样做:
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:Access-Control-Allow-Origin
controls which Cross Origin request headers it will accept. You need to set theAccess-Control-Allow-Origin
header on the server to accept an Origin ofhttp://127.0.0.1:8000
.On Node Express server you do it like this: