简体   繁体   English

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

[英]Could not parse request body into json: Unrecognized token

I have a very simple AJAX code that's calling an AWS API gateway endpoint:我有一个非常简单的 AJAX 代码调用 AWS API 网关端点:

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

And what I am getting back is:我得到的回报是:

Could not parse request body into json: Unrecognized token 'zipcode': was expecting ('true', 'false' or 'null')`无法将请求正文解析为 json:无法识别的令牌“邮政编码”:期待(“真”、“假”或“空”)`

The data should be in JSON format so What am I doing wrong?数据应该是 JSON 格式,所以我做错了什么?

I have also tried:我也试过:

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

And they are returning the same result.他们返回相同的结果。

That fixed it:修复它:

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

I changed the integration request to Proxy integration (in API GW Choose the method, go to integration request window and choose "Use Lambda Proxy integration") and it works!我将集成请求更改为代理集成(在 API GW 中选择方法,转到集成请求窗口并选择“使用 Lambda 代理集成”)并且它有效!

please remove ' from key like:请从密钥中删除 ',例如:

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

This is usually a problem that comes from AWS Lambda and how you have the API Gateway setup.这通常是来自 AWS Lambda 以及您如何设置 API 网关的问题。 Your code looks fine.您的代码看起来不错。 I would check how you have set the integration on API Gateway.我会检查您是如何在 API 网关上设置集成的。

I would check in your integration setup on APIGateway to make sure you have something like:我会检查你在 APIGateway 上的集成设置,以确保你有类似的东西:

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

in your template mapping to deal with the JSON.在您的模板映射中处理 JSON。

Your client code is perfectly fine, the issue is with the endpoint implementation, server is not able to parse the json.您的客户端代码非常好,问题在于端点实现,服务器无法解析 json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CloudFormation YAML State 机器:无法识别令牌的 INVALID_JSON_DESCRIPTION - CloudFormation YAML State Machine: INVALID_JSON_DESCRIPTION for unrecognized token Data Fusion 无法解析来自 JSON 的响应 - Data Fusion could not parse response from JSON 在 CLI 中使用嵌套的 json 请求正文创建云调度程序作业 - Create a cloud scheduler job in CLI with nested json request body AWS Application Load Balancer 无法处理内容编码为 gzip 且内容类型为 application/json 的请求正文 - AWS Application Load Balancer fails to process request body for content encoding gzip and content type application/json 如何用axios写请求体? - How to write request body with axios? Cloud Front:无法满足请求 - Cloud Front : The request could not be satisfied 请求正文未到达 golang 光纤 - The request body does not reach golang fiber 部署 Firebase 和 Node.js 应用程序导致错误:Error: Parse Error in remoteconfig.template.json: Unexpected token 'i' at 1:1 - Deploying Firebase and Node.js application results in error: Error: Parse Error in remoteconfig.template.json: Unexpected token 'i' at 1:1 Lambda 正在请求正文中返回标头 - Lambda is returning headers in body of request AWS api 网关在响应正文中返回令牌 - AWS api gateway returning token in the response body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM