ajax - Request JSON to load into Javascript object. Differences in response -
take @ function first, notice json file located locally:
if (request) { request.open("get", "flicks.json", true); request.onreadystatechange = function() { if (request.readystate == 4) { var testi = request.responsetext; var listmovies = json.parse(testi); console.log(typeof testi); // returns string console.log(typeof listmovies); // returns object - yay! } }; request.send(null); }
that's perfect, got wanted, object json data in it.
but when changed location of json file , put on server:
request.open("get", "http://www.myserver.com/flicks.json", true);
then comes error:
syntaxerror: json.parse: unexpected end of data
example json
{ "feed": "....", "description": "fake list of netflix movies", "modified": "2010-10-25t15:04:46z", "generator": "i did it", "items": [ { "title": "...", "link": "h...", "media": { "m": "..." }, "date_taken": "..." }, { "title": "...", "link": "...", "media": { "m": "..." }, "date_taken": "..." } ] }
why work locally not other way?
if put: http://www.myserver.com/flicks.json
in browser, can see data? (is file reachable). second guess, sure files identical(local vs server)?
edit 1: try this. slight change in order of calls.
if (request) { request.onreadystatechange = function() { if (request.readystate == 4) { var testi = request.responsetext; var listmovies = json.parse(testi); console.log(typeof testi); // returns string console.log(typeof listmovies); // returns object - yay! } }; request.open("get", "http://www.myserver.com/flicks.json", true); request.send(null); }
Comments
Post a Comment