javascript - How to get JSON array on client_JS from server_JS? -


from nodejs (with express) try send json_array in response client js:

asksjsonarray = json.parse(fs.readfilesync("tasks.json", 'utf-8'));  app.get('/getarr', function (req, res) {     readjsoncontent();     res.json(json.stringify(tasksjsonarray)); //sending json array client_js in response }); 

on client-side want it, nothing receive:

$.get('/getarr').success(function(res) {   var currencydata = json.parse(res);   if (!currencydata.rates) {     // possibly handle error condition unrecognized json response     alert("currency data not found!");   } else {     taskarr = currencydata;   } }) 

so receive msg 'currency data not found!' ...

res.json converts data json, don't have manually:

res.json(tasksjsonarray); 

i believe set appropriate headers, on client, don't have explicitly parse json, jquery you:

$.get('/getarr').done(function(currencydata){   if (!currencydata.rates) {     // possibly handle error condition unrecognized json response     alert("currency data not found!");   } else {     taskarr = currencydata;   } }); 

please note assigning response free variable not useful since won't know when it's "safe" access variable. might want have @ how return response asynchronous call? .

this may still not work since currencydata might value not have rates property. learn how correctly access data, have @ access / process (nested) objects, arrays or json.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -