【问题标题】:CORS did not succeed? How can i prevent that?CORS没有成功?我怎样才能防止这种情况?
【发布时间】: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 没有成功的错误。我该如何预防呢?

【问题讨论】:

标签: c# reactjs nginx axios


【解决方案1】:

前端没有适合您的解决方案。这是出于安全目的。您需要做的是在您的 .NET 后端 API 服务器中允许 cors。

你可以从这里学习 https://www.c-sharpcorner.com/article/cors-in-dotnet-core/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多