Read JSON with jQuery -


i want load json file , read following data

{     "exttitle": {         "message": "test1"     },     "extname":{         "message": "test2"     } } 

this how load data

function loadlocales(){     var userlang = (navigator.language) ?                      navigator.language : navigator.userlanguage;      switch(userlang){         default:             $.getjson("_locales/ne/messages.json", function(data){                 return data;             });         break;     } } 

when try read following function

i18n undefined error 

.

function getvalue(key){     var i18n = loadlocales();     return i18n[key].message; } 

any ideas?

this because ajax asynchronous. not possible return success callback. (there "sychronous" option, out of question series of dictionary lookups.)

you need re-build program's flow operation (whatever - populating label or other element value example) takes place in success callback of ajax call.

 $.getjson("_locales/ne/messages.json", function(data){             // example, populate element item json            $("#element").html(data.message);         }); 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -