【问题标题】:‘content-type’ is not allowed according to header ‘Access-Control-Allow-Headers’ from CORS preflight response根据 CORS 预检响应中的标头“Access-Control-Allow-Headers”,不允许使用“content-type”
【发布时间】:2020-09-28 07:12:08
【问题描述】:

我正在尝试进行一个简单的邮寄电话,将数据发送到 mailgrid,以便将自动邮件发送给选定的人。但是由于我对进行 API 调用比较陌生,所以有些事情我不明白。

这些是我尝试拨打电话时遇到的错误:

根据 CORS 预检响应中的标头“Access-Control-Allow-Headers”,不允许标头“内容类型”

CORS 请求未成功

他是我执行调用的函数:

const sendMail = () => {
        axios.post(`https://{{private_url}}/public/mail/{user_id}/{mail_id}`, {
            headers: {
                "Access-Control-Allow-Headers": "Content-Type",
            },
            data: {
                first_name: data.first_name,
                last_name: data.last_name,
                message: data.message,
                email: data.email,
            }
        })
            .then(response => {
                console.log(response);
                console.log(response.headers)
                close();
            })
            .catch(error => {
                console.log(error);
                console.log(error.headers)
            });;
    }

我在监督什么或者我在哪里犯了错误?

【问题讨论】:

    标签: javascript reactjs api axios


    【解决方案1】:

    您错误地使用了 headers 对象。 content-type 是独立的。请像这样使用:

    headers: {
        "Access-Control-Allow-Headers": "*", // this will allow all CORS requests
        "Access-Control-Allow-Methods": 'OPTIONS,POST,GET', // this states the allowed methods
        "Content-Type": "application/json" // this shows the expected content type
    },
    

    【讨论】:

    • 感谢您清除它!但我仍然遇到同样的错误
    • 您遇到了什么错误?这是CORS问题吗?您需要在服务器上启用它。如果它不是您的服务器,那么您将无法做任何事情,因为这是一种安全措施
    • 还是这个错误:Reason: header ‘content-type’ is not allowed according to header ‘Access-Control-Allow-Headers’ from CORS preflight response
    • 另外,我有一个 CORS 插件,当我激活它时,我的 api 调用可以正常工作。 CORS Everywhere 是 FF 的插件
    • 您需要将该标头添加到服务器代码中,然后在响应中。你拥有服务器吗?
    【解决方案2】:

    developer.mozilla 所说:

    使用 CORS 的 HTTP 请求失败,因为 HTTP 连接在网络或协议级别失败。该错误与 CORS 没有直接关系,而是某种基本的网络错误。

    在许多情况下,它是由浏览器插件(例如广告拦截器或 隐私保护器)阻止请求或 VPN 插件更改请求的来源。

    如果有,你可以禁用它。

    服务器还可以阻止导致 Access-Control-Allow-Origin 错误的未知来源。

    【讨论】:

      猜你喜欢
      • 2017-02-19
      • 2019-03-17
      • 2016-05-15
      • 2021-03-15
      • 2020-02-27
      • 2018-01-26
      • 2011-06-29
      • 2021-06-06
      相关资源
      最近更新 更多