当前位置: 首页 > 知识库问答 >
问题:

Access-Control-Allog-Headers在飞行前响应中不允许请求头字段content-type

舒斯伯
2023-03-14

CORS策略阻止了从来源'http://localhost:3000'获取'My InvokeURL'的访问:在飞行前响应中,Access-Control-Allow-Headers不允许请求头字段content-type。

我的目标是将POST请求取到API网关。我在网关控制台的选项响应中添加了Access-Control-Allow-Headers标头,其值为'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-AMZ-Security-Token',但实际响应中根本没有这个字段。

响应标头:

access-control-allow-methods: POST,OPTIONS
access-control-allow-origin: *
content-length: 36
content-type: application/json
date: Mon, 28 Oct 2019 21:13:22 GMT
status: 200
module.exports.sendM = function (event, context, callback) {
console.log(event);
var body =`<p>Olá, ${event.name}!</p><p>Esse é o resumo do seu pedido: </p>`;

let minion1 = event.minion;
let minion2 = event.minion2;
let minion3 = event.minion3;

if (event.minion1){
    body += `<p> Au Naturel: ${event.minion1}</p>`;
}
 if (event.minion2){
    body += `<p> Phil: ${event.minion2}</p>`;
}
 if (event.minion3){
    body += `<p> Bored Silly Kevin: ${event.minion3}</p>`;
}
body += `<p> Enviar para ${event.address}</p>`;

var mailOptions = {
    from: 'leosole@gmail.com',
    subject: 'Pedido minionshop',
    html: body,
    to: `${event.mail}`
    // bcc: Any BCC address you want here in an array,
};


// create Nodemailer SES transporter
var transporter = nodemailer.createTransport({
    SES: ses
});
const response = {
    statusCode: 200,
    headers: {
      "Access-Control-Allow-Origin": "*", // Required for CORS support to work
      "Access-Control-Allow-Credentials": true, // Required for cookies, authorization headers with HTTPS
      "Access-Control-Allow-Headers": "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
      "Access-Control-Allow-Methods": "POST, OPTIONS"
    },
    body: JSON.stringify(event)
};
// send email
console.log(event.mail);
transporter.sendMail(mailOptions, function (err, info) {
    if (err) {
        console.log("Error sending email");
        callback(err);
    } else {
        console.log("Email sent successfully");
        callback(null, response);
    }
});};
handleSubmit = event => {
event.preventDefault();
console.log('trying to fetch')
fetch('myInvokeUrl', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
    // 'Access-Control-Allow-Origin': '*'
  },
  body: JSON.stringify({
    name: this.state.name,
    mail: this.state.mail,
    address: this.state.address,
    minion1: this.state.minion1,
    minion2: this.state.minion2,
    minion3: this.state.minion3
  })
})
.then(function(response) {
return response.json()
}).then(function(json) {
  console.log('parsed json', json)
}).catch(function(ex) {
  console.log('parsing failed', ex)
})}

我的集成响应

共有1个答案

杨骁
2023-03-14

导航到您的方法响应post方法(而不是选项),并检查是否有以下配置:

然后导航到您的Integration Response post方法(而不是选项),并检查是否有以下配置:

先在没有发送电子邮件功能的情况下尝试,只在发送电子邮件之前回调(null,{response});

 类似资料: