2016-10-28 58 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'); 
    }, 
});