c# - CORS没有成功?我怎样才能防止这种情况?
问题描述
我想向在 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没有成功的错误。我怎样才能防止这种情况?
解决方案
前端没有适合您的解决方案。这是出于安全目的。您需要做的是在您的 .NET 后端 API 服务器中允许 cors。
你可以从这里学习 https://www.c-sharpcorner.com/article/cors-in-dotnet-core/