Can I save the data and status of the Jquery POST / GET callback function in a variable for later use?

advertisements

I want to save the value of data and status in a variable and use it after the closing brackets of jquery GET/POST function.But alert comes only when it is inside .get braces.

$(document).ready(function(){
    $.get("demo_test.asp",function(data,status){
         v = data;
     });
 alert("Data:"+v);
 });


As Jasper said, your alert is being triggered before the request is complete (async!). So, you have two options:

Do your logic inside the callback:

$.get("demo_test.asp",function(data,status){
     v = data;
     alert("Data:"+v);
     //Process stuff here
});

Or pass the received data onto another function and work with it there

$.get("demo_test.asp",function(data,status){
     v = data;
     doStuff(v);
});

function doStuff(param) {
    console.log(param);
}