tags:

views:

60

answers:

1

Hi all,

New to json/jQuery so sorry if this has an obvious answer.

I'm doing an ajax request in jQuery that's something like:

$.ajax({
        url: theURL,
        dataType: 'jsonp',
        type: 'get',
        success: function(data) {
            alert("it's there");
        }
    });

The request asks whether a given object is in a database. If it is, it returns something of the format:

    {
    "text": "duck", 
    "canonical_name": "duck", 
    "language": {
        "id": "en"
    }}

However, if the object isn't there, it returns:

Not Found

As in...literally that exact string, not in any kind of json format as far as I know. Is there any way I can get my ajax to detect this? Right now it doesn't even seem to be acknowledging that it got anything back in the latter case.

The json code wasn't written by me. It can possibly be fixed if this is not the correct format and there's absolutely nothing I can do from my end to work with this, but I'd really like to try to find some kind of workaround if possible.

Thanks very much!

A: 

You remove the dataType option and in callback :

success: function(data) {
        alert("it's there");
        var myJSON = eval(data);

    }

Now you can get the data as an object and use like :

myJSON.language.id
Aneesh
the only issue with this is in the case that this JSON can come from an unreliable source, in which case you won't want to use eval(), but a third-party json library to safely parse the data. if you know where all the information is coming from however, there is no problem!
Lowgain