2013-06-22 44 views
4

我寫了下面的JavaScript來創建在谷歌任務列表:「谷歌API的消息」:「此API不支持解析形式編碼的輸入。」

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." } 
} 

可能的錯誤是什麼?

回答

0

您發送的數據,如:

title=Netsuite%20List 

但谷歌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數據。

0

不工作

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