1

I am trying to set up a simple interface to test the Basecamp API. I have set my dataType = jsonp to avoid the cross-domain issue. When making the call I can see in the inspector that the response is sending back properly formatted JSON. However my error alerts show 4 and 200 but then the response text is 'undefined'. I am assume I am not properly converting from the jsonp to json but do I need to given I get the response I want? Or am I not accessing the response correctly.

Code:

function findAllProjects() {
    console.log('findAllProjects');
    $.ajax({
        type: 'GET',
        url: rootURL + "projects.json",
        username: "username",
        password: "password",
        crossDomain: true,
        //contentType: "application/json",
        dataType: "jsonp", // data type of response

        success: function(data) {
            alert(data[0].id);
            console.log("Success function!");
            console.log(data);
        },

        error: function(xhr, err) {
            //alert("Error!");
            alert("readyState: "+ xhr.readyState+"\nstatus: "+ xhr.status);
            alert("responseText: "+ xhr.responseText);
        },
    });
}
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
porterhaus
  • 439
  • 2
  • 5
  • 17

2 Answers2

0

Your URL is for a .json file, but JSONP requires a JavaScript file that is specially formatted to pass the result into a function you control (or, managed by jQuery since you're using it). Either way, the server needs to support JSONP, you can't just request a JSON file and coerce it into a JSONP interaction.

jmar777
  • 38,796
  • 11
  • 66
  • 64
  • The API returns only json. I have it set to jsonp because that's the only way I can get it to execute. I am getting back a json response with data. – porterhaus Jan 20 '14 at 17:01
  • 1
    @porterhaus then it is not possible to request said data using only client-side code, unless you modify the server-side response to either support CORS, or JSONP. currently it appears to support neither. – Kevin B Jan 20 '14 at 17:19
  • Thanks. I have concluded the same after researching a few of the other forumns. They are eventually planning support for it. – porterhaus Jan 20 '14 at 17:24
0

Server side, needs to wrap the jason data with a function call. The name of the function is passed in the url as "?callback=?".

So instead of just returning your json object {...} fromt he server, you need to return

SomeFunctionName({...});
Ceres
  • 3,524
  • 3
  • 18
  • 25
  • Is that a function of the server side response or can I request it as a matter of my request? – porterhaus Jan 20 '14 at 17:04
  • jQuery is passing the name of the function in the request using the callback parameter in the url. SomeFunctionName should be what is in the callback parameter and is part of the server side response. – Ceres Jan 20 '14 at 17:10