简体   繁体   中英

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:

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.

I've researched all over for how to solve this, and the response would be: "Return the data in jsonp format".. well. It's an external API and they don't provide data in JSONP. Is there a way I could override the returned function and parse this JSON data anyway?

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

As elektronik pointed out the dataType should be json and not 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');
    },
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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