php - Get global array of arrays from Jquery $.getJSON method to REUSE array -
okay after searching stack overflow answers found 2 threads , tried them both did not work me.
assign data jquery getjson array
my issues want work data database mapping utility, elements of json dissappear after call jquery method $.getjson(). can create them 'ul' element fine. cannot them push array of array's method have tried yet. asking here curious doing wrong. have thought may have traverse dom elements once populated maybe seems silly me , wondering if there easier way potentially. novice @ javascript , jquery understand programming concepts.
the concepts using this:
i have sql server 2008 database values:
placeid placename 1 place 1 2 place 2 3 place 3 4 place 4
i can create php script 'sqlsrv' driver values , output
echo json_encode($data);
i can confirm can return data php script in iis locally. (with special jerry rigging have iis make php)
i can call php script , display it's data jquery library 2.0.3.js using the
$.getjson('sqltalk.php')
i can populate elements in html this, cannot array of array's reuse other objects, really want. want make entire javascript gets array of arrays php script , reference javascript either embedded or reference use mapping. appears $.getjson asychrnonous reading methods seeing not working me attempt 'push' existing array. may misunderstanding syntax when mixing jquery , traditional javascript though.
complete html code bottleneck is. php returning data shown in step 1 except json object. simple example me beyond proof of concept first. can push explicitly array, , can generate child items of ul element fine. cannot push items 'getjson' @ no matter attempt @ reformating getjson method have read.
<html> <head> <script type='text/javascript' src='jquery 2.0.3.js'></script> </head> <body> <ul></ul> <script> test = [ { placeid: "1", placename: "somewhere" } ] test.push({placeid: "2", placename: "somewhereelse"}); function getarray() { return $.getjson('sqltalk.php') } getarray().done(function(json){ $.each(json, function(key, val){ //test[key] = { placeid: val.placeid}; // doesn't work test.push({placeid: val.placeid, placename: val.placename}); // not work, can't push. $('ul').append('<li id="' + key + '">' + val.placename + '</li>'); }); }); alert(test.length); // output 'ul' element fine, test array never gets values. //i have tried others have stated , not work. </script> </body> </html>
the global not updated until after request complete.
var getarraypromise = getarray().done(function(json){ $.each(json, function(key, val){ //test[key] = { placeid: val.placeid}; // doesn't work test.push({placeid: val.placeid, placename: val.placename}); // not work, can't push. $('ul').append('<li id="' + key + '">' + val.placename + '</li>'); }); }); getarraypromise.done(function(){ alert(test.length); });
there no workaround, that's how asynchronous requests work. clock keeps ticking , code keeps executing while request being received.
Comments
Post a Comment