【问题标题】:Could not parse request body into json: Unrecognized token无法将请求正文解析为 json:无法识别的令牌
【发布时间】:2017-07-29 06:17:08
【问题描述】:

我有一个调用 AWS API 网关端点的非常简单的 AJAX 代码:

$.ajax({
        url: 'https://omitted.execute-api.ap-southeast-2.amazonaws.com/test/rec',
        type: 'post',
        data: {
            'zipcode': '1234',
            'url': 'www.google.com'
        },
        dataType: 'json',
        success: function (data) {
            console.info(data);
        }
    });

我得到的是:

无法将请求正文解析为 json:无法识别的令牌 'zipcode':期待 ('true'、'false' 或 'null')`

数据应该是 JSON 格式所以我做错了什么?

我也试过了:

$.post('https://omitted.execute-api.ap-southeast-2.amazonaws.com/test/rec',
    {
        'zipcode': '1234',
        'url': 'www.google.com'
    }, 
    function(data, textStatus) {
      //data contains the JSON object
      //textStatus contains the status: success, error, etc
}, "json");

$.post('https://omitted.execute-api.ap-southeast-2.amazonaws.com/test/rec',
    'zipcode=1234&url=www.google.com', 
    function(data, textStatus) {
      //data contains the JSON object
      //textStatus contains the status: success, error, etc
}, "json");

它们返回的结果相同。

【问题讨论】:

  • 这通常是由 AWS Lambda 和您如何设置 API Gateway 引起的。你的代码看起来不错。我会检查您如何在 API Gateway 上设置集成。
  • @Mark_M 尽管 Mark 在邮递员那里工作得很好,所以我怀疑问题不应该出在 AWS 上,对吧?
  • 是的,这会让我觉得端点没问题……

标签: ajax aws-api-gateway


【解决方案1】:

解决了它:

$.postJSON = function(url, data, callback) {
    return jQuery.ajax({
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    'type': 'POST',
    'url': url,
    'data': JSON.stringify(data),
    'dataType': 'json',
    'success': callback
    });
};

【讨论】:

    【解决方案2】:

    请从键中删除 ',例如:

    data: {
            zipcode: '1234',
            url: 'www.google.com'
        },
    

    【讨论】:

      【解决方案3】:

      您的客户端代码非常好,问题在于端点实现,服务器无法解析 json。

      【讨论】:

        【解决方案4】:

        这通常是来自 AWS Lambda 以及您如何设置 API Gateway 的问题。你的代码看起来不错。我会检查您如何在 API Gateway 上设置集成。

        我会检查你在 APIGateway 上的集成设置,以确保你有类似的东西:

        {"body-json" : $input.json('$')}
        

        在你的模板映射中处理 JSON。

        【讨论】:

        • 我们现在在 AWS 控制台上,在哪里可以找到或添加它?
        • 这在 API Gateway 的方法集成选项卡中。如果您单击它打开的资源下的 POST。它位于“集成请求”下的“身体映射模板”中。设置有点烦人。很多人使用 Lambda 代理集成来避免处理这个问题。
        • 好的,我们进去并添加了一个新的内容类型application/json 然后我们进入模板,我们在哪里添加该行?
        • 如果你添加了一个内容类型,你应该可以点击它。它将在下面打开一个空白文本区域。你会想要上面的东西(我编辑它以包括大括号)。
        • 好的,现在我们有 2 个不同的错误,一个来自邮递员:{"stackTrace": [["/var/task/lambda_function.py", 41, "lambda_handler", "if event['zipcode'] in postcode_dict:"]], "errorType": "KeyError", "errorMessage": "'zipcode'"},另一个来自 ajax 请求:{"message": "Could not parse request body into json: Unrecognized token \'zipcode\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: [B@5e1e9e24; line: 1, column: 9]"}
        【解决方案5】:

        我将集成请求更改为代理集成(在 API GW 中选择方法,转到集成请求窗口并选择“使用 Lambda 代理集成”)并且它可以工作!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-01
          • 2021-01-16
          • 1970-01-01
          • 2023-03-10
          相关资源
          最近更新 更多