9

I've written a simple generic ajax function that can be called by multiple functions in my script. I'm not sure how to get the data returned to the ajax function back to the caller.

// some function that needs ajax data
function myFunction(invoice) {
    // pass the invoice data to the ajax function
    var result = doAjaxRequest(invoice, 'invoice');
    console.dir(result); // this shows `undefined`
}

// build generic ajax request object
function doAjaxRequest(data, task) {
    var myurl = 'http://someurl';
    $.ajax({
        url: myurl + '?task=' + task,
        data: data,
        type: 'POST',
        success: function(data) {
            console.dir(data); // this shows good data as expected  
            return data; // this never gets back to the calling function
        }
    });
}

Is there a way to return the ajax data to the calling function?

6
  • 2
    No. The calling function has finished running before the AJAX call is even initiated. You could make it synchronous... but then it's not AJAX. Commented Oct 10, 2012 at 22:44
  • 1
    Couldn't he pass a (possibly anonymous) callback function to the doAjaxRequest function and then invoke it from the success closure? Commented Oct 10, 2012 at 22:45
  • @TheZ-- crap-- obvious now. Thanks for the help with a dumb question! Commented Oct 10, 2012 at 22:46
  • @KevinBoucher He could, but that wouldn't be passing it back to the calling function as a return. That would be a callback. Commented Oct 10, 2012 at 22:46
  • @Z: agreed, but it might be a solution to his overall issue. Commented Oct 10, 2012 at 22:53

2 Answers 2

15

$.ajax is asynchronous, so in order to get the data back you will need to pass a callback to your doAjaxRequest function. I've added a callback parameter to doAjaxRequest and instead of using the result of doAjaxRequest the code that handles the response is in the callback function.

// some function that needs ajax data
function myFunction(invoice) {
    // pass the invoice data to the ajax function
    doAjaxRequest(invoice, 'invoice', function (result) { 
        console.dir(result);
    });
}

// build generic ajax request object
function doAjaxRequest(data, task, callback) {
    var myurl = 'http://someurl';
    $.ajax({
        url: myurl + '?task=' + task,
        data: data,
        type: 'POST',
        success: function(data) {
            console.dir(data); // this shows good data as expected  
            callback(data);
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

-1

i think help full to you this example

function testAjax() {
    var result="";
    $.ajax({
      url:"getvalue.php",
      async: false,  
      success:function(data) {
         result = data; 
      }
   });
   return result;
}

function fun2()
{

alert(testajax())

}

you notice one thing async: false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.