Micky
Micky

Reputation: 175

Storing ajax response array into a variable for later usage

Hi I need to store an AJAX Response into two variables x and y or into a array. My AJAX response is a array. I am able to see the data but only with and alert into che call. I need the data outside the ajax call

var x;
var y;

$.ajax({
    url: 'ajaxload.php',
    dataType: "json",
    success: function (data) {
        x = data.posX;
        y = data.posX;
        alert(x + " " + y);  // I can se data but I need outside ajax call
    }
});

Upvotes: 9

Views: 68288

Answers (4)

tftd
tftd

Reputation: 17062

If I understand correctly, you want to reuse the ajax response later within your code. If that's the case, your current code wouldn't work because by default, the javascript engine doesn't wait for the response of ajax requests. In other words the code below won't work:

<script type="text/javascript">
$(document).ready(function(){
    var x; 
    var y;
    $.ajax({
        url: 'ajaxload.php',
        dataType: "json", 
        success: function(data) { 
            x= data.posX;
            y= data.posX;
            alert (x+" "+y);  // I can se data but I need outside ajax call
        }
    });
    alert(x+" "+y); // You won't see anything, because this data isn't yet populated. The reason for this is, the "success" function is called when the ajax request has finished (it has received a response).
})
</script>

You need to wait for the ajax response. To do that with jQuery you need to slightly modify your code:

<script type="text/javascript">
$(document).ready(function(){
    var data = $.parseJSON($.ajax({
        url:  'ajaxload.php',
        dataType: "json", 
        async: false
    }).responseText); // This will wait until you get a response from the ajax request.

    // Now you can use data.posX, data.posY later in your code and it will work.
    var x = data.posX;
    var y = data.posY;
    alert(x+" "+y);
    alert(data.posX+" "+data.posY);
});
</script>

Upvotes: 34

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38552

you can store the ajax response in a global array for further use in other javascript function

var ajaxResult=[];

$(document).ready(function(){

  $.ajax({
    url: 'ajaxload.php',
    async:true,
    dataType: "json", 
    success: function(data)
     { 
        ajaxResult.push(data);
     }
  });
});

otherJsfunc()
 {
  console.log(ajaxResult); 
 }

Upvotes: 22

user1726343
user1726343

Reputation:

If you are running this code within a document ready handler, your x and y variables are not truly global. Try window.x=''; window.y='';

Upvotes: 0

Nevin
Nevin

Reputation: 205

if you decalre the variable you can access the value out side of ajax.

eg:

<script type="text/javascript">
   var x = '';
   var y = '';

   function sendAjax()
   {
        // your ajax call
        x= data.posX;
        y= data.posX;  
   } 
   sendAjax();
</script>

if the ajax works good you can acces the variable x and y globaly

Upvotes: 0

Related Questions