开发者

jQuery AJAX JSON dataType Conversion

Hopefully that title isn't too cryptic. What's happening is I have a jQuery AJAX script that I'm trying to use to access an API on a remote server, which returns a JSON response. However, the API returns the JSON as MIME type "text/html" (in the response header) instead of "application/json". It would seem obvious that I simply need to change the returned content type from text to JSON, to make the AJAX call interpret the data correctly.

Unfortunately, this is not开发者_Python百科 the case. I have tried this in a multitude of different ways, all of which fail. The closest I've gotten to getting this API call to work is when the debugger tells me "Resource interpreted as Script but transferred with MIME type text/html". And the AJAX call errors out with my debug message that dumps the jqXHR object in JSON format, which tells me: {"readyState":4,"status":200,"statusText":"parsererror"}

Here is an example of my code (although I have changed the code many various ways, in my attempts at getting it to work, but this version seems to be the closest to correct):

$.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 anyone can figure out what I need to do differently to make this work, I will be extremely grateful.

It may also be worth noting that if you copy/paste the API URL into a browser address bar and hit go, it gives the proper JSON response with the proper response header ("application/json")


So unless Kanbanpad updates their API, it cannot be directly accessed with JS. You will have to use PHP (or some other) to handle the requests.

It works just as well, it just requires an extra step is all.

Just for anyone that was looking for a solution.


    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. Bear in mind this affects all ajax calls, so use info from data to decide which ones you want to convert.


Verify that your server is sending a jsonp response. This means the json should be enclosed with a string of your callback.

The callback name is passed in the parameters, and if you're not setting it explicitly, looks something like: jQuery15102810791094068532_1300988427891 (As per http://www.json-p.org/)

On your server, you need to format the response:

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

Where you use the callback defined in your GET parameter 'callback'.

You might try setting the type to "json" and see if it works. I've had a number of parsererror's with the jquery's jsonp - you might try http://code.google.com/p/jquery-jsonp until it's a bit smoother.


Try changing your content-type to this

                contentType: "application/json; charset=utf-8",
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜