2016-10-28 59 views
3

我處理的todoist API(https://developer.todoist.com/)和我正在爲某些數據與這個jQuery的AJAX GET請求:如何在jQuery ajax調用上將JSON響應解析爲JSONP?

var url = "https://todoist.com/API/v7/sync"; 
var data = { 
    'token' : token, 
    'resource_types' : '["all"]', 
    }; 


$.ajax({ 
    url: url, 
    data: data, 
    type: 'GET', 
    dataType: 'jsonp', 
    success: function(response) { 

     console.log(response); 

    }, 
    error: function(response) { 
     console.log('error'); 
    }, 
}); 

現在,當我得到的迴應,我得到的錯誤

Unexpected token : 

爲什麼?因爲根據(https://stackoverflow.com/a/7941973/2724978)jQuery期待jsonp格式的響應,但它返回json。

我已經研究瞭如何解決這個問題,響應將是:「以jsonp格式返回數據」..好。它是一個外部API,它們不提供JSONP中的數據。有沒有辦法我可以覆蓋返回的函數並解析這個JSON數據呢?

+0

'token'定義?鏈接文檔建議使用POST請求_「同步API請求應在HTTP POST(application/x-www-form-urlencoded)中進行。包含錯誤的同步API響應將以JSON返回。 – guest271314

+0

它被定義,是 – raphadko

回答

0

您的dataType應該是json,而不是jsonp

+0

似乎是正確的。剛剛測試過我的自我...謝謝! –

0

正如elektronik指出dataType應該是json而不是jsonp。該代碼比看起來如下...

var token = "your token" 
var url = "https://todoist.com/API/v7/sync"; 
var data = { 
    'token' : token, 
    'resource_types' : '["all"]', 
    }; 

jQuery.ajax({ 
    url: url, 
    data: data, 
    type: 'GET', 
    dataType: 'json', 
    success: function(response) { 
     console.log(response); 
    }, 
    error: function(response) { 
     console.log('error'); 
    }, 
});