17

How can I return the value "pinNumber" from jquery ajax so I can append it outside of the ajax. Here is my code

var x = pinLast + 1;
    for(i=x;i<=pinMany;i++) {
        var i = x++;
        var cardNumber = i.toPrecision(8).split('.').reverse().join('');
        var pinNumber = '';

        jQuery.ajax({
            type: "POST",
            url: "data.php",
            data: "request_type=generator",
            async: false,
            success: function(msg){
                var pinNumber = msg;
                return pinNumber;
                //pin number should return
            }
        });

        jQuery('.pin_generated_table').append(cardNumber+' = '+pinNumber+'
'); // the variable pinNumber should be able to go here }

Please ask me if you don't understand.. ^^ thanks

2
  • 3
    Yet another "how do I return data from an asynchronous function" question. This kind of question is asked all too often. Voting to close. Commented Feb 13, 2011 at 7:22
  • How dare you ask this question! I vote to remove this person from the earth. Commented Aug 18, 2015 at 20:43

6 Answers 6

15

AJAX is asynchronous by default, you cannot return a value from the callback without making a synchronous call, which you almost certainly don't want to do.

You should supply a real callback function to the success: handler, and put your program logic there.

Sign up to request clarification or add additional context in comments.

3 Comments

oh thanks so much.. i already got the idea.. :) by putting my varibles inside of the ajax then append it in the inside.
I don't really see the need for callbacks in SYNC AJAX.
Sync Ajax is an oxymoron. And sync XHR sucks.
5
var pinNumber = $.ajax({
    type: "POST",
    url: "data.php",
    data: "request_type=generator",
    async: false
}).responseText;
jQuery('.pin_generated_table').append(cardNumber+' = '+pinNumber+' ');

1 Comment

Pls dont use this whoever will read this!!! This is WRONG! Our programmer now thinks that ajax is working like this!
4

It has to do with variable scope. The local variable pinNumber you create is not accessible outside its wrapping function.

Perhaps declare pinNumber globally or if it'll do the trick, simply stick your .append() inside your success function.

3 Comments

It has nothing to do with variable scope; the .append is executing before the success callback.
async is false though, meaning .append will run after the AJAX call.
OP doesn't need to declare the pinNumber globally, it will suffice to not use the var keyword in the assignment to it inside the success handler. The assignment will then affect the enclosing scope.
3
var _successEvent = function(response){
    $('.pin_generated_table').append(cardNumber + ' = ' + response);
};

$.ajax({
    type: "POST",
    url: "data.php",
    data: "request_type=generator"
}).done(_successEvent);

Comments

1

You can use this example:

window.variable = 'some data';

This make you variable global and you can access to this from anywhere

1 Comment

But when would the implementer know when the value set for window.variable is changed and accessible with the new value?
-3

Here is the simple solution.

    //---------------------------Ajax class ------------------------------//
    function AjaxUtil()
    {
            this.postAjax = function(Data,url,callback)
            {
              $.ajax({
                 url: url,
                 async: true,     //must be syncronous request! or not return ajax results
                 type: 'POST',
                 data: Data,
                 dataType: 'json',
                 success: function(json)
                 {
                     callback(json);
                 }
               });
            }//--end postAjax
    }
    //--------------------------------End class--------------------//
    var ajaxutil = new AjaxUtil();

    function callback(response)
    {
          alert(response); //response data from ajax call
    }

  var data={};
  data.yourdata = 'anydata';
  var url = 'data.php';

  ajaxutil.postAJax(data,url,callback); 

2 Comments

Your comment on async: true is simply lying and wrapping the callback in another function is pointless. Besides, this is unnecessary. What does wrapping it into a class have to do with the question?
Regardless of any of that he's the only one that gave a proper example of the use of success:. The accepted "answer" isn't even an answer as it doesn't give an example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.