Ivanka
Ivanka

Reputation: 3

problem using jquery getjson method

I am trying to use json output in jquery method.

$(function() {
    $.getJSON("/items/list/", function(json) {
        var source = json;
        alert(source.os[0]);
    });
});

It does not work. But when I directly goto the url(/items/list/), I see the json output. It looks something like this..

{"os":["Windows","Chrome","Mac OS X"], "languages":["php", "Java"]}

I appreciate any help.

Thanks.

Upvotes: 0

Views: 369

Answers (2)

suren
suren

Reputation: 981

If you are aware of Firefox Firebug addon that might help you .

Goto the script tab, just keep a breakpoint in the 4th line that is var source = json; and have a look at the value of source in the right side of the firebug.

If the above doesn't help, you can try this jQuery.parseJSON( json ) which converts JSON string and returns JavaScript object.

Upvotes: 0

Krule
Krule

Reputation: 6476

Perhaps mime type for json is not set in header before outputting:

Try:

$(function() {
    $.getJSON("/items/list/", function(json) {
        var source = $.parseJSON(json);
        alert(source.os[0]);
    });
});

Upvotes: 2

Related Questions