代码之家  ›  专栏  ›  技术社区  ›  Naguib Ihab

无法将请求正文解析为json:无法识别的令牌

  •  4
  • Naguib Ihab  · 技术社区  · 8 年前

    $.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”)`

    我也尝试过:

    $.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");
    

    5 回复  |  直到 8 年前
        1
  •  1
  •   Naguib Ihab    8 年前

    这解决了它:

    $.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
  •  1
  •   HolyM    6 年前

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

        3
  •  0
  •   Muhammad Shahabipour    8 年前

    data: {
            zipcode: '1234',
            url: 'www.google.com'
        },
    
        4
  •  0
  •   Abhijeet Ahuja    8 年前

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

        5
  •  0
  •   Mark    8 年前

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

    我会在APIGateway上检查您的集成设置,以确保您具有以下内容:

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

    推荐文章