javascript - How to store ajax JSON object into global variable to be accessed outside the function -


how store ,success function returned data global variable.

$("form").on("submit", function () {     var data = {         "action": "test"     };     data = $(this).serialize() + "&" + $.param(data);     $.ajax({         type: "post",         datatype: "json",         url: "ajax2.php",         data: data,         success: function (data) {             //how store above data global variable?             $("#main_content").slideup("normal", function () {                 (i = 0; < data.length; i++) {                     $(".the-return").append("<div class='inside_return'>name:"                              + data[i].name + "<br/>id:"                              + data[i].id + "<br/>pricing:"                              + data[i].rate + "<br/>postcode:"                              + data[i].postcode + "<br/>reputation:"                              + data[i].reputation + "<br/>review plus:"                              + data[i].plus + "<br/>review negative:"                              + data[i].neg + "<br/><h1>availability</h1>week morning:"                              + data[i].weekm + "<br/>week afternoon:"                              + data[i].weeka + "<br/>week evening:"                              + data[i].weeke + "<br/>weekend morning:"                              + data[i].endm + "<br/>weekend afternoon:"                              + data[i].enda + "<br/>week evening:"                              + data[i].ende + "</div>");                     //alert(data[i].name)                  }             }); //closes #main_content         }     });     return false; }); 

this ajax performed when search form submitted. after php query returned result ,i want enable user sort data of json object. in order this, need store object('data' in case) global variable.it' because have function make use of data perform sorting!

for define global variable

var newdata; $("form").on("submit", function () {.... //your code ... success: function (data) {    newdata = data; //assign `data` global variable here   .... 

now can access newdata outside ajax.


Comments