Javascript 如何通过Ajax在jQuery中将请求主体传递给AWS Lambda?

Javascript 如何通过Ajax在jQuery中将请求主体传递给AWS Lambda?,javascript,node.js,ajax,aws-lambda,aws-api-gateway,Javascript,Node.js,Ajax,Aws Lambda,Aws Api Gateway,我有一个简单的AWS Lambda函数,如下所示: exports.handler = async function (event, context) { console.log('Received event:', event); context.done(null, JSON.stringify({"status": "success"})); }; 它只记录事件。 我的主要问题是,当我使用jQueryAjax发送请求时,我没有得到事

我有一个简单的AWS Lambda函数,如下所示:

exports.handler = async function (event, context) {
    console.log('Received event:', event);
    context.done(null, JSON.stringify({"status": "success"}));
};
它只记录事件。 我的主要问题是,当我使用jQueryAjax发送请求时,我没有得到事件体,但是当我使用Postman时,我得到了事件体。 以下是jQuery代码:

var name = $("#name-input").val();
var phone = $("#phone-input").val();
var email = $("#email-input").val();
var desc = $("#description-input").val();
var data = {
    fullName : name,
    phone : phone,
    email : email,
    desc : desc
};

$.ajax({
    type: "POST",
    URL : <AWS API Gateway URL that invokes the Lambda function>,
    crossDomain: "true",
    contentType: "application/json",
    data: JSON.stringify(data),
    success: function () {
         alert("Successful!");
    },
    error: function () {
         alert("Unsuccessful!");
    }});
});
var name=$(“#名称输入”).val();
var phone=$(“#phone input”).val();
var email=$(“#电子邮件输入”).val();
var desc=$(“#描述输入”).val();
风险值数据={
全名:姓名,
电话:电话,,
电邮:电邮,,
描述:描述
};
$.ajax({
类型:“POST”,
网址:,
跨域:“真”,
contentType:“应用程序/json”,
数据:JSON.stringify(数据),
成功:函数(){
警报(“成功!”);
},
错误:函数(){
警报(“不成功!”);
}});
});
以下是我使用jQuery Ajax时在CloudWatch上获得的事件日志:

{
    "version": "2.0",
    "routeKey": "ANY /<Lambda Function Name>",
    "rawPath": "/default/<Lambda Function Name>",
    "rawQueryString": "",
    "headers": {
        "accept": "*/*",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "en-US,en;q=0.9",
        "access-control-request-headers": "content-type",
        "access-control-request-method": "POST",
        "content-length": "0",
        "host": <AWS API GATEWAY URL>,
        "origin": "http://localhost",
        "referer": "http://localhost/",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "cross-site",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36",
        "x-amzn-trace-id": "Root=1-5fc4a14a-1225b6fe203f78495dca8fef",
        "x-forwarded-for": <MY IP ADDRESS>,
        "x-forwarded-port": "443",
        "x-forwarded-proto": "https"
    },
    "requestContext": {
        "accounted": <AWS ACCOUNT ID>,
        "apiId": <AWS API GATEWAY ID>,
        "domainName": <AWS API GATEWAY URL>,
        "domainPrefix": <AWS API GATEWAY ID>,
        "http": {
            "method": "OPTIONS",
            "path": "/default/<Lambda Function Name>",
            "protocol": "HTTP/1.1",
            "sourceIp": <MY IP ADDRESS>,
            "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36"
        },
        "requestId": "Wz4juhL_IAMESnw=",
        "routeKey": "ANY /<Lambda Function Name>",
        "stage": "default",
        "time": "30/Nov/2020:07:37:46 +0000",
        "timeEpoch": 1606721866934
    },
    "isBase64Encoded": false
}
{
“版本”:“2.0”,
“routeKey”:“任何/”,
“rawPath”:“/默认值/”,
“rawQueryString”:“,
“标题”:{
“接受”:“*/*”,
“接受编码”:“gzip,deflate,br”,
“接受语言”:“en-US,en;q=0.9”,
“访问控制请求头”:“内容类型”,
“访问控制请求方法”:“POST”,
“内容长度”:“0”,
“主持人”:,
“来源”:http://localhost",
“推荐人”:http://localhost/",
“sec fetch dest”:“空”,
“秒提取模式”:“cors”,
“sec获取站点”:“跨站点”,
“用户代理”:“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/87.0.4280.66 Safari/537.36”,
“x-amzn-trace-id”:“根=1-5fc4a14a-1225b6fe203f78495dca8fef”,
“x-for”:,
“x转发端口”:“443”,
“x-forwarded-proto”:“https”
},
“请求上下文”:{
“会计”:,
“Apid”:,
“域名”:,
“域名前缀”:,
“http”:{
“方法”:“选项”,
“路径”:“/默认/”,
“协议”:“HTTP/1.1”,
“源IP”:,
“用户代理”:“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/87.0.4280.66 Safari/537.36”
},
“请求ID”:“Wz4juhL_IAMESnw=”,
“routeKey”:“任何/”,
“阶段”:“默认”,
“时间”:“2020年11月30日:07:37:46+0000”,
“时间纪元”:1606721866934
},
“isBase64Encoded”:false
}
下面是我使用Postman时在CloudWatch上获得的事件日志:

{
    "version": "2.0",
    "routeKey": "ANY /<Lambda Function Name>",
    "rawPath": "/default/<Lambda Function Name>",
    "rawQueryString": "",
    "headers": {
        "accept": "*/*",
        "accept-encoding": "gzip, deflate, br",
        "cache-control": "no-cache",
        "content-length": "116",
        "content-type": "application/json",
        "host": <API GATEWAY URL>,
        "postman-token": "8d8f269e-da07-4bc8-987e-07bcd35bf587",
        "user-agent": "PostmanRuntime/7.22.0",
        "x-amzn-trace-id": "Root=1-5fc49fbc-0d95e3bd63009b4b6767b1c1",
        "x-forwarded-for": <MY IP ADDRESS>,
        "x-forwarded-port": "443",
        "x-forwarded-proto": "https"
    },
    "requestContext": {
        "accountId": "701682778102",
        "apiId": <AWS APIGATEWAY ID>,
        "domainName": <AWS API GATEWAY URL>,
        "domainPrefix": <AWS APIGATEWAY ID>,
        "http": {
            "method": "POST",
            "path": "/default/<Lambda Function Name>",
            "protocol": "HTTP/1.1",
            "sourceIp": <MY IP ADDRESS>,
            "userAgent": "PostmanRuntime/7.22.0"
        },
        "requestId": "Wz3ldieTIAMEVcg=",
        "routeKey": "ANY /<Lambda Function Name>",
        "stage": "default",
        "time": "30/Nov/2020:07:31:08 +0000",
        "timeEpoch": 1606721468439
    },
    "body":
        "{\n" +
        "    \"name\": \"Test user\",\n" +
        "    \"phone\": \"+xxxxxxxxxx\",\n" +
        "    \"email\": \"testuser@example.com\",\n" +
        "    \"desc\": \"test message\"\n" +
        "}",
    "isBase64Encoded": false
}
{
“版本”:“2.0”,
“routeKey”:“任何/”,
“rawPath”:“/默认值/”,
“rawQueryString”:“,
“标题”:{
“接受”:“*/*”,
“接受编码”:“gzip,deflate,br”,
“缓存控制”:“无缓存”,
“内容长度”:“116”,
“内容类型”:“应用程序/json”,
“主持人”:,
“邮差代币”:“8d8f269e-da07-4bc8-987e-07bcd35bf587”,
“用户代理”:“PostmanRuntime/7.22.0”,
“x-amzn-trace-id”:“根=1-5fc49fbc-0D95E3BD6309B4B67B1C1”,
“x-for”:,
“x转发端口”:“443”,
“x-forwarded-proto”:“https”
},
“请求上下文”:{
“帐户ID”:“701682778102”,
“Apid”:,
“域名”:,
“域名前缀”:,
“http”:{
“方法”:“发布”,
“路径”:“/默认/”,
“协议”:“HTTP/1.1”,
“源IP”:,
“userAgent”:“PostmanRuntime/7.22.0”
},
“请求ID”:“Wz3ldieTIAMEVcg=”,
“routeKey”:“任何/”,
“阶段”:“默认”,
“时间”:“2020年11月30日:07:31:08+0000”,
“时间纪元”:1606721468439
},
“正文”:
“{\n”+
“\”名称\“:\”测试用户\“,\n”+
“\”电话\“:\”+XXXXXXXXX\”,\n”+
“\“电子邮件\”:\”testuser@example.com\“,\n”+
“\“desc\”:\“测试消息\”\n”+
"}",
“isBase64Encoded”:false
}
从上面的日志中可以看到,当我使用Postman时,我可以得到事件主体,但是当我使用jQueryAjax时,我没有得到主体。 请告诉我在使用jQuery Ajax时如何获取事件体。 提前谢谢你