0

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.

2
  • 1
    What do you mean "it does not work"? Commented May 3, 2011 at 11:30
  • 1
    Hum , it seems to work for me. It shows "Windows" as it could. Commented May 3, 2011 at 11:31

2 Answers 2

2

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]);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.