4

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?

Community
  • 1
  • 1
sigmaxf
  • 7,998
  • 15
  • 65
  • 125
  • Is `token` defined? The linked documentation recommends using `POST` request _"Sync API requests should be made in HTTP POST (application/x-www-form-urlencoded). Sync API responses, including errors, will be returned in JSON."_? – guest271314 Oct 28 '16 at 05:57
  • It is defined, yes – sigmaxf Oct 28 '16 at 07:12

2 Answers2

0

Your dataType should be json, not jsonp.

Nick Ribal
  • 1,959
  • 19
  • 26
0

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');
    },
});
Benjamin Ziepert
  • 1,345
  • 1
  • 15
  • 19