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

Nag*_*hab 5 ajax aws-api-gateway

我有一个非常简单的 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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

我得到的回报是:

无法将请求正文解析为 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");
Run Code Online (Sandbox Code Playgroud)

他们返回相同的结果。

Nag*_*hab 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
    });
};
Run Code Online (Sandbox Code Playgroud)