2

I have an ajax call. I put the return value (data) in a variable called mydata. I would like to use that variable outside the ajax call. Do note that I can not incorporate my code in the success part of the ajax call. I will not detail the reasons as it will add confusion. So the question is simple. Is there a way to use the return value of an ajax call outside that ajax call? Thank you in advance for your replies. Cheers. Marc

$.ajax({
    type: "POST",
    url: "file.php",
    data: ajaxData,
    success: function(data) {
        var mydata = data;    
    }
});

alert(mydata); // -> This is not working
2
  • Just declare var mydata in the scope that you need it (i.e. outside the call).
    – m90
    Apr 2, 2012 at 9:17
  • var mydata = null; $.ajax({ type: "POST", url: "file.php", data: ajaxData, success: function(data) { mydata = data; } }); alert(mydata);
    – user285594
    Apr 2, 2012 at 9:17

4 Answers 4

3

Short answer: No you can't, as A in AJAX stands for "Asynchronous", so the script doesn't "wait" for the AJAX call to complete.

You can, if you want, use an interval polling to see whether mydata is set:

var mydata = null;

$.ajax({
    type: "POST",
    url: "file.php",
    data: ajaxData,
    success: function(data) {
        var mydata = data;    
    }
});

var timerId = setInterval(function() {
   if(mydata !== null) {
      // your code
      clearInterval(timerId);
   }
}, 1500); // change this to your liking, doesn't really matter

But this assumes that file.php will never returns null in data

5
  • thank you nifty for the acknowledgment...
    – Marc
    Apr 2, 2012 at 9:23
  • 2
    this is bad workarround. If ajax call dont return on success it will infinite loop
    – Yorgo
    Apr 2, 2012 at 9:27
  • If ajax call doesn't return, then mydata will always be null and mydata !== null will fail and it will immediately returns from the function every 1.5 seconds... Apr 2, 2012 at 9:29
  • This is an awful solution to a well solved problem. Use callbacks or promises, do not use a polling loop.
    – user229044
    Jul 4, 2013 at 12:33
  • @meagar my reasoning is that the limitation imposed upon by OP: Do note that I can not incorporate my code in the success part of the ajax call. I will not detail the reasons as it will add confusion. I fully understand this is a bad workaround which I don't normally encourage :s. Jul 5, 2013 at 2:37
2

Well, I don't recommend to use global variables in any case, although I'm not sure what do you want to do there, you might want to wrap your data process within a function.

That code is working fine, it is just async and your variable data is getting filled later. Try something like this this to see your data on the console:

var process(data) {
    // output your data
    console.log(data);
}

$.ajax({
    type: "POST",
    url: "file.php",
    data: ajaxData,
    success: process
});
1

By not working, do you mean you are getting undefined or object in the alert?

In your example data will be a json object returned. Therefore you need to know the structure of this object to be able to use it.

For example if the structure is:

data = {name = "Bob", location = "London"}

then data.name would give you the value Bob.

Also the mydata variable is local to the jquery post function, you would need to define it outside the function to use it outside the function.

0

no you can not set the callback value to js global variable. You have to use callback values inside the callback stack.

Not the answer you're looking for? Browse other questions tagged or ask your own question.