javascript - JSON object null in browser but not firebug -
when step through javascript code (scoreboard.js) in firebug, works fine alerts. when don't put line break in firebug , run normally, received "favs null" message (no alerts).
var favs = $.getjson("favs.json"); favs = $.parsejson(favs.responsetext); favs = favs.myteams; (i=0; i<favs.length; i++){ alert(favs[i].text); }
the json (favs.json)
{"myteams":[{"sport":10,"id":10,"abbrev":"nyy","isfav":false,"text":"new york yankees","sw_abbrev":"nyy"},{"sport":28,"id":19,"abbrev":"nyg","isfav":false,"text":"new york giants","sw_abbrev":"nyg"},{"sport":46,"id":18,"abbrev":"ny","isfav":false,"text":"new york knicks","sw_abbrev":"nyk"},{"sport":90,"id":11,"abbrev":"nj","isfav":false,"text":"new jersey devils","sw_abbrev":"njd"},{"sport":41,"id":2507,"abbrev":"prov","isfav":false,"text":"providence friars"},{"sport":46,"id":17,"abbrev":"nj","isfav":false,"text":"new jersey nets","sw_abbrev":"njn"},{"sport":600,"id":363,"abbrev":"blues","isfav":false,"text":"chelsea","sw_abbrev":"eng.chelsea","isnational":false}]}
the html
<html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="scoreboard.js"></script> </head> <body> test </body>
any idea going on here?
you don't need
favs = $.parsejson(favs.responsetext);
and i'm pretty sure $.getjson()
doesn't return json, returns jqxhr.
looking @ documentation parsejson(), should doing:
jquery.getjson( url [, data] [, success(data, textstatus, jqxhr)] )
$.getjson("favs.json", function(json) { var favs = json.myteams; (i=0; i<favs.length; i++){ alert(favs[i].text); } });
$.getjson()
- success callback (in example anonymous function) passed returned data, typically javascript object or array defined json structure , parsed using $.parsejson() method.
Comments
Post a Comment