Getting JSON locally using JavaScript/jQuery -
i have read several questions here stating, json file cannot loaded locally in google chrome due security policy etc. found solution on http://youmightnotneedjquery.com/.
my javascript looks this:
jquery(document).ready(function($){ var items = []; var settingsfile = "corr/settings.json"; var sidepanel = $(document.createelement('div')).addclass('corrpanel').appendto('body'); /** * gets json file * @param string jsonfile * @return json file contents */ function getsettings(jsonfile) { request = new xmlhttprequest(); request.open('get', jsonfile, true); request.onload = function() { if (request.status >= 200 && request.status < 400) { jsonrows = json.parse(request.responsetext); console.log(jsonrows); $.each(jsonrows, function(key, row) { items.push("<input type='text' class='iris' id='" + row.section + "'>"); }); console.log(items); } else { console.log("error getting json file"); } }; request.send(); } getsettings(settingsfile); console.log(items); });
console.log returns output of json file without problem.
so, works well, since new javascript (and don`t know how work functions , properties there), data property not set (and returned) result property undefined well.
could tell me how pass request result outside of function?
thanks
edit: added pushing results items array, available in local context of getsettings function, last console.log has not output.
the whole problem tried access items property directly. request setting called after page loaded. moved logic success part.
if (request.status >= 200 && request.status < 400) { // execute success code , manage items here ... }
Comments
Post a Comment