problem using jquery getjson method
I am trying to use json output in jquery method.
$(functi开发者_StackOverflowon() {
$.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.
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]);
});
});
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.
精彩评论