【发布时间】:2021-06-29 09:02:19
【问题描述】:
我想向在 Nginx 服务器中工作的 .NET API 发送获取请求。但我无法得到任何回应,只有 cors 错误。
axios.get(`http://localhost:${port}/${api}`)
.then((res) => {
console.log(res);
response = res;
});
当我发送这样的请求时。它给了我这个错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows
reading the remote resource at
我已经稍微更改了我的代码。就是这样:
axios.get(`http://localhost:${port}/${api}`, {
headers: {
"Access-Control-Allow-Origin": true,
"Access-Control-Allow-Headers": "Content-Type",
},
withCredentials: true,
})
.then((res) => {
console.log(res);
response = res;
});
它给了我 CORS 没有成功的错误。我该如何预防呢?
【问题讨论】:
-
"Access-Control-Allow-Origin": true,... true 不是有效的 URL。