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
Marc
  • 9,217
  • 21
  • 67
  • 90
  • Just declare `var mydata` in the scope that you need it (i.e. outside the call). – m90 Apr 02 '12 at 09:17
  • `var mydata = null; $.ajax({ type: "POST", url: "file.php", data: ajaxData, success: function(data) { mydata = data; } }); alert(mydata);` –  Apr 02 '12 at 09:17

4 Answers4

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

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • thank you nifty for the acknowledgment... – Marc Apr 02 '12 at 09:23
  • 2
    this is bad workarround. If ajax call dont return on success it will infinite loop – Yorgo Apr 02 '12 at 09: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... – Andreas Wong Apr 02 '12 at 09:29
  • This is an awful solution to a well solved problem. Use callbacks or promises, do *not* use a polling loop. – user229044 Jul 04 '13 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. – Andreas Wong Jul 05 '13 at 02: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
});
natos
  • 21
  • 2
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.

Peter Campbell
  • 661
  • 1
  • 7
  • 35
0

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

Yorgo
  • 2,668
  • 1
  • 16
  • 24