根据 CORS 预检响应中的标头“Access-Control-Allow-Headers”,不允许使用“content-type”

Yor*_*örn 4 javascript api reactjs axios

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

\n\n

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

\n\n
\n

根据 CORS 预检响应中的标头 \xe2\x80\x98Access-Control-Allow-Headers\xe2\x80\x99,不允许标头 \xe2\x80\x98content-type\xe2\x80\x99

\n
\n\n

\n\n
\n

CORS 请求未成功

\n
\n\n

他是我执行调用的函数:

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

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

\n

Red*_*ron 6

您错误地使用了 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
},
Run Code Online (Sandbox Code Playgroud)

  • 仍然出现此错误:“原因:根据 CORS 预检响应中的标头“Access-Control-Allow-Headers”,不允许使用标头“content-type”” (2认同)