Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的fetch命令没有将正文传递给AWS API网关?_Javascript_Amazon Web Services_Fetch_Aws Api Gateway - Fatal编程技术网

Javascript 为什么我的fetch命令没有将正文传递给AWS API网关?

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", "

我在AWSAPI网关中有一个阶段,它接受
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();
}