简体   繁体   English

如何在jQuery ajax调用上将JSON响应解析为JSONP?

[英]How to parse a JSON response as JSONP on a jQuery ajax call?

I'm dealing with the todoist API ( https://developer.todoist.com/ ) and I am making a jquery ajax get request for some data with this: 我正在处理todoist API( https://developer.todoist.com/ ),我正在使用以下方法对某些数据进行jquery ajax get请求:

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

Now, when I get the response, I get the error 现在,当我收到回复时,我收到错误

 Unexpected token : 

Why? 为什么? Because according to ( https://stackoverflow.com/a/7941973/2724978 ) jQuery is expecting a jsonp formatted response, but it returns json. 因为根据( https://stackoverflow.com/a/7941973/2724978),jQuery期望一个jsonp格式的响应,但它返回json。

I've researched all over for how to solve this, and the response would be: "Return the data in jsonp format".. well. 我已经研究了如何解决这个问题,响应将是:“以jsonp格式返回数据”......好吧。 It's an external API and they don't provide data in JSONP. 它是一个外部API,它们不提供JSONP中的数据。 Is there a way I could override the returned function and parse this JSON data anyway? 有没有办法可以覆盖返回的函数并解析这个JSON数据?

您的dataType应该是json ,而不是jsonp

As elektronik pointed out the dataType should be json and not jsonp . 正如elektronik指出的那样,dataType应该是json而不是jsonp The code than looks as following ... 代码看起来如下......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM