javascript - Assign value to global variable using $.Ajax (JQuery) -


i creating new functionality build grid based on json data returned ajax. have decided want encapsulate functionality within function, when add/update/delete, can on success retrieve new representation of data.

the problem having want fill global array once function uses ajax ends, have array no data. isn't problem when code within ajax call once try separate in own function, doesn't work intended.

 <script type="text/javascript">     var dataarray = [];      // use function fill array     function retrievenotes() {         $.ajax({             url: "http://wks52025:82/wcfdataservice.svc/getnotesfromview()?$format=json",             type: "get",             datatype: "json",             asynch:true,             success: function (data) {                 returneddata = data;                 $.each(data.d, function (i, item) {                     dataarray[i] = [];                     dataarray[i][0] = item.notestitle.trim();                     dataarray[i][1] = item.profilename.trim();                     dataarray[i][2] = item.isshared;                     dataarray[i][3] = item.nameofuser.trim();                 }) // end of each loop             }         });     }       $(document).ready(function () {         retrievenotes();         dataarray;     </script> 

it's asynchronous, you'll have wait ajax call finish before can use data :

function retrievenotes() {     return $.ajax({         url: "http://wks52025:82/wcfdataservice.svc/getnotesfromview()?$format=json",         type: "get",         datatype: "json"     }); }  $(document).ready(function () {     retrievenotes().done(function(data) {         var dataarray = [];         $.each(data.d, function (i, item) {             dataarray[i] = [];             dataarray[i][0] = item.notestitle.trim();             dataarray[i][1] = item.profilename.trim();             dataarray[i][2] = item.isshared;             dataarray[i][3] = item.nameofuser.trim();         });         // can use data inside done() handler,         // when call has completed , data returned     }); }); 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -