Kshitij Pandey Kshitij Pandey - 6 months ago 16
Ajax Question

How can I access AJAX defined variable gloabally?

I created a variable in AJAX success function which I want to use for another AJAX call outside that function. I tried to store it with local storage and then use it but could not access the variable. Any idea as to how can I access it?

Here is the code:-

var c_id = 110;
$.ajax({
type : "POST",
dataType : 'json',
data : {'id':c_id},
url : 'http://127.0.0.1/Student Back End/sbms.php',
success : function(data){
var quant_presta = data.quantity;
localStorage.setItem('Presta_Quant',quant_presta);
console.log(localStorage.getItem('Presta_Quant'));
},
error : function(data){
//console.log(data.name.quantity);
$('#fetch_error').show();
$('#fetch_error').fadeOut(5000);
}
});

Answer

Show the code ..

$.ajax({
 url : '',
 success: function(data){
  myFunction(data);
 }
});

function myFunction(data){
  // do the processing here. You can access this from all ajax calls.
}