Yor*_*örn 4 javascript api reactjs axios
我正在尝试进行一个简单的 post 调用,将数据发送到 mailgrid,以便自动将邮件发送给所选人员。但由于我对 API 调用还比较陌生,所以有些事情我不明白。
\n\n这些是我尝试拨打电话时遇到的错误:
\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\nCORS 请求未成功
\n
他是我执行调用的函数:
\n\nconst 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您错误地使用了 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)
归档时间: |
|
查看次数: |
12413 次 |
最近记录: |