Amazon web services AWS API网关和;Lambda&x2013;没有从文本到application/json的转换

Amazon web services AWS API网关和;Lambda&x2013;没有从文本到application/json的转换,amazon-web-services,lambda,aws-lambda,aws-api-gateway,Amazon Web Services,Lambda,Aws Lambda,Aws Api Gateway,我创建了一个Lambda函数并添加了一个API网关触发器。添加触发器将创建一个API作为“Lambda代理”和“代理资源”。这很好,因为根据代理资源“将Lambda函数的输出转换为HTTP响应”。但是,它返回200,但调用ajax请求的error函数。调试错误时,我看到parserror和没有从文本到应用程序/json的转换。API网关中是否有我缺少的东西?我尝试将application/json=>Empty添加到“方法响应”中,但没有成功 以下是Lambda函数返回的结果: return {

我创建了一个Lambda函数并添加了一个API网关触发器。添加触发器将创建一个API作为“Lambda代理”和“代理资源”。这很好,因为根据代理资源“将Lambda函数的输出转换为HTTP响应”。但是,它返回
200
,但调用ajax请求的error函数。调试错误时,我看到
parserror
没有从文本到应用程序/json的转换。API网关中是否有我缺少的东西?我尝试将
application/json=>Empty
添加到“方法响应”中,但没有成功

以下是Lambda函数返回的结果:

return {
    "statusCode": 200,
    "headers": {
        "Access-Control-Allow-Origin": "*",
        "Content-Type": "application/json"
    },
    "body": "Email added to system."
}
以下是AJAX:

  var formData = $("#submitEmail").serializeArray();
  console.log(formData);

  $.ajax({
      url : "https://<REDACTED>.execute-api.us-east-1.amazonaws.com/prod/SubmitEmail",
      type: "POST",
      data : formData,
      dataType: "application/json",
      success: function(data, textStatus, xhr)
      {
          console.log("SUCCESS")
          console.log(xhr.status)
      },
      error: function (xhr, textStatus, errorThrown)
      {
          console.log("ERROR")
          console.log(xhr.status)
          console.log(textStatus)
          console.log(errorThrown)
      }
  });
var formData=$(“#submitEmail”).serializeArray();
console.log(formData);
$.ajax({
url:“https://.execute-api.us-east-1.amazonaws.com/prod/SubmitEmail",
类型:“POST”,
数据:formData,
数据类型:“应用程序/json”,
成功:函数(数据、文本状态、xhr)
{
console.log(“成功”)
console.log(xhr.status)
},
错误:函数(xhr、textStatus、errorshown)
{
console.log(“错误”)
console.log(xhr.status)
console.log(textStatus)
console.log(错误抛出)
}
});

我不太确定如何解释这一点,但我能够让它调用AJAX请求的success函数。我认为是数据类型不匹配。这是我改变的

修改了我的ajax请求:

  var formData = {
    email: $("#email").val()
  }

  dataType: "json"  (instead of "application/json")
Lambda函数:

return {
    "statusCode": 200,
    "headers": {
        "Access-Control-Allow-Origin": "*"
    },
    "body": json.dumps({
        "msg": "Email added to system."
    })
}

对于API网关,“200的响应头”和“200的响应体”下没有列出任何内容

不确定它是否能解决问题,但您能否尝试将body设置为
“body”:{msg:“电子邮件已添加到系统。”}
?如果内容类型为
application/json
,则发送对象而不是字符串更有意义。是的,
json.parse(“添加到系统的电子邮件”)
会抛出错误。您需要实际提供有效的JSON.Hmm,这会破坏函数并给我一个502。如果您查看的最底部,
body
需要一个字符串。@sambol您需要执行
“body”:JSON.stringify({msg:“Email added to system.”})