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); }, }); } 
2
  • you can't send a username/password with a jsonp request other than as url parameters. Commented Jan 20, 2014 at 16:54
  • stackoverflow.com/questions/3295692/… Commented Jan 20, 2014 at 16:54

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 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.
Thanks. I have concluded the same after researching a few of the other forumns. They are eventually planning support for it.
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({...}); 

2 Comments

Is that a function of the server side response or can I request it as a matter of my request?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.