4

我编写了以下 javascript 来在 google 中创建任务列表:

postData = {'title':'Netsuite List'};
access_token = 'xxxx';

url = 'https://www.googleapis.com/tasks/v1/users/@me/lists';

headers['Content-type'] = 'application/json';
headers['Authorization'] = 'Bearer ' + access_token;
headers['Content-length'] = 25; 
response = $$.requestURL(url, postData, headers, 'POST');

回复说:

{ "error": 
{ "errors": [ { "domain": "global", "reason": "parseError", "message": "This API does not support parsing form-encoded input." } ], "code": 400, "message": "This API does not support parsing form-encoded input." } 
}

可能的错误是什么?

4

3 回答 3

2

不工作

contentType: 'application/json; charset=UTF-8',

试试这个

var headers = { };

headers["Content-Type"] ="application/json ; charset=UTF-8"; 
//remove to parsing form-encoded input error

data:JSON.stringify( model),
//this use for remove to parse error

例子:

$.ajax({
    type: 'Post',
    url: postUrl,
    headers: headers,
    dataType: 'json',//not required in some case
    data:JSON.stringify( model),                
    success: function (data, sts) {
        alert('success');
    },
    error: function (err, sts) {
        var msg;
    }
});
于 2017-12-06T07:06:51.350 回答
0
jQuery.ajax({
    url: "https://www.googleapis.com/tasks/v1/users/@me/lists",
    method: "POST",
    data: JSON.stringify({ /* your object */ }),
    dataType: "json",
    beforeSend: (xhr) => {
        xhr.setRequestHeader("Content-Type", "application/json");
    },
    //...

或者 :

jQuery.ajax({
    url: "https://www.googleapis.com/tasks/v1/users/@me/lists",
    method: "POST",
    data: JSON.stringify({ /* your object */ }),
    dataType: "json",
    contentType: "application/json",
    //...
于 2019-04-02T14:23:30.187 回答
0

您发送的数据如下:

title=Netsuite%20List

但是 Google API 等待JSON

{ "title": "Netsuite List" }

尝试JSON.stringify()为该方法提供输出requestURL

postData = JSON.stringify({'title':'Netsuite List'});          // <-- Added JSON.stringify
access_token = 'xxxx';

url = 'https://www.googleapis.com/tasks/v1/users/@me/lists';

headers['Content-type'] = 'application/json';
headers['Authorization'] = 'Bearer ' + access_token;
headers['Content-length'] = 25; 
response = $$.requestURL(url, postData, headers, 'POST');

此外,最好绕过$$您使用的文档或对象的来源,并检查它如何支持发送 JSON 数据。

于 2017-06-23T04:51:15.800 回答