JQuery AJAX JSON dataType Conversion

I hope this name is not too mysterious. What happens, I have a jQuery AJAX script that I am trying to use to access an API on a remote server that returns a JSON response. However, the API returns JSON as the MIME type "text / html" (in the response header) instead of "application / json". It would seem that I just need to change the returned content type from text to JSON in order to force the AJAX call to correctly interpret the data.

Unfortunately, this is not so. I tried this in many different ways, all of which fail. The closest I got to get this API call to work is when the debugger tells me "Resource interpreted as a script, but passed with text like MIME / html". And the AJAX error causes an error with my debug message, which unloads the jqXHR object in JSON format, which tells me: {"readyState":4,"status":200,"statusText":"parsererror"}

Here is an example of my code (although I changed the code in many different ways, trying to make it work, but this version seems to be the closest one):

 $.ajax({ type: 'GET', url: 'http://username:api-key@www.kanbanpad.com/api/v1/projects.json', contentType: 'application/json', dataType: 'jsonp', converters: { 'jsonp': jQuery.parseJSON, }, success: function(data) { alert(data); }, error: function(jqXHR, textStatus, errorThrown) { console.log(JSON.stringify(jqXHR)); console.log(textStatus+': '+errorThrown); } }); 

If someone can understand what I need to do differently to make this work, I will be extremely grateful.

You can also note that if you copy / paste the API URL into the address bar of the browser and press "go", it gives the correct JSON response with the corresponding response header ("application / json")

+8
jquery types jsonp ajax
source share
4 answers

Therefore, if Kanbanpad does not update its API, it cannot be directly accessed using JS. You will need to use PHP (or some others) to process requests.

It works just as well, it just takes an extra step.

Just for those who are looking for a solution.

+1
source share
  dataFilter(data, type)Function A function to be used to handle the raw response data of XMLHttpRequest. This is a pre-filtering function to sanitize the response. You should return the sanitized data. The function accepts two arguments: The raw data returned from the server and the 'dataType' parameter. 

I would change the content type in the dataFilter interceptor to json. Remember that this affects all ajax calls, so use the information from the data to decide which ones you want to convert.

0
source share

Make sure your server sends a jsonp response. That means json should be wrapped in your callback chain.

The callback name is passed in the parameters, and if you do not set it explicitly, it looks something like this: jQuery15102810791094068532_1300988427891 (according to http://www.json-p.org/ )

On your server you need to format the response:

 jQuery15102810791094068532_1300988427891({...json response ...}); 

If you use the callback defined in your GET parameter, the callback.

You can try setting the type to "json" and see if it works. I had several parsererror with jquery jsonp - you can try http://code.google.com/p/jquery-jsonp until it becomes smoother.

0
source share

Try changing your content type to

  contentType: "application/json; charset=utf-8", 
-one
source share

All Articles