Javascript 为什么我的fetch命令没有将正文传递给AWS API网关?
我在AWSAPI网关中有一个阶段,它接受Javascript 为什么我的fetch命令没有将正文传递给AWS API网关?,javascript,amazon-web-services,fetch,aws-api-gateway,Javascript,Amazon Web Services,Fetch,Aws Api Gateway,我在AWSAPI网关中有一个阶段,它接受POST请求。如果我用Postman调用这个API网关,一切都会正常工作。我可以在CloudWatch日志中看到,帖子的主体是存在的。正文如下(简称): 我还为Postman中的内容类型设置了一个标题:content-type:application/json 我尝试在JavaScript中使用fetch再次进行相同的调用: let testJson = { "Success": "1", "
POST
请求。如果我用Postman调用这个API网关,一切都会正常工作。我可以在CloudWatch日志中看到,帖子的主体是存在的。正文如下(简称):
我还为Postman中的内容类型设置了一个标题:content-type:application/json
我尝试在JavaScript中使用fetch
再次进行相同的调用:
let testJson = {
"Success": "1",
"Item": {
"IngameName": "SomeName",
"Timestamp": "Mon Oct 12 2020 19:07:29 GMT+0100 (British Summer Time)"
}
};
fetch(apiGatewayAddressLocal, {
method: 'POST',
body: JSON.stringify(testJson),
headers: {
'Content-Type': 'application/json'
}
});
fetch
调用成功到达API网关,但CloudWatch日志显示正文为空。我也尝试过做json.parse
而不是json.stringify
,但这在控制台中给了我意外的标记
错误。我看到了一些解决方案,包括Accept:application/json
作为标题,我尝试了这些解决方案,但没有修复该解决方案。我也试过了,但是我根本无法到达API网关
我仔细检查了apiGatewayAddressLocal是否确实包含有效链接,它与我在Postman中使用的链接相同
为什么我的
fetch
调用没有向API网关传递任何正文?发生此问题的原因是fetch
命令未包含在async
函数中,而fetch
命令需要与wait
一起使用。我还使用fetch
操作在no-cors
模式中添加了
async function someFunction(callback) {
let testJson = {
"Success": "1",
"Item": {
"IngameName": "SomeName",
"Timestamp": "Mon Oct 12 2020 19:07:29 GMT+0100 (British Summer Time)"
}
};
await fetch(apiGatewayAddressLocal, {
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(testJson)
});
callback();
}
async function someFunction(callback) {
let testJson = {
"Success": "1",
"Item": {
"IngameName": "SomeName",
"Timestamp": "Mon Oct 12 2020 19:07:29 GMT+0100 (British Summer Time)"
}
};
await fetch(apiGatewayAddressLocal, {
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(testJson)
});
callback();
}