1

I'm trying to build a jQuery ajax function which is totally generic. It would receive all the data and even request type as parameters. After that it will substitute in the respective variables and build the request. Here is the code...

function server_request (type, url, dataType, data) {
    var packet;
    var response = null;

    if (packet) {
        packet.abort();
    };

    if (type == "get") {
        packet = $.ajax({ type: type, url: url, dataType: dataType });
        packet.done(function(returned_data){
              response = returned_data;
        });
    }
    return response;
    response = null;
}

So I want the received data to be stored in an already declared variable called "response" and I want it to be returned for use in another place. Somehow its not working and it keeps returning the predefined value of the response variable. can someone help??

mbz
  • 50
  • 6
  • the done, happens in another context, so the return resposne will be null. You could do a blocking ajax call before but it obosolete now because it caused perfonace issues. What you need to do is provide a callback into your generic mehtod, and bind to that callback on the function calling it, return the resposne in the packet.done code... not in the server_request code. – Piotr Kula Aug 26 '15 at 19:32
  • Yes, it's a duplicated answer: see the suggested link http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – gaetanoM Aug 26 '15 at 19:45
  • @gaemaf if this is a duplicate of an original question am I supposed to delete this or something?.(I'm new on this site). – mbz Aug 26 '15 at 19:48
  • @mbz You don't have to do anything. The question stays up so it can help provide greater visibility to other people who may search for it. – Dave Aug 26 '15 at 20:00

2 Answers2

2

Ajax requests are asynchronous, so by the time you use response somewhere, it might now have value yet.

You should probably return the whole packet and do a packet.done() call where you need the response at.

Swiffy
  • 4,401
  • 2
  • 23
  • 49
  • YOU are one AMAZING PERSON man!!!!!!!!!!!!!! you just made my day :D :D :D THANK YOU SO MUCH!!! I hope you have an awesome day (even month, year, whole life)! – mbz Aug 26 '15 at 19:43
  • This is actually pretty cool too. I don't know you can do that ^^ – Piotr Kula Aug 26 '15 at 19:59
1
function server_request (type, url, dataType, data, _callBack) {

    var packet;
    //var response = null; //not needed

    if (packet) {
        packet.abort();
    };

    if (type == "get") {
        packet = $.ajax({ 
             type: type, 
             url: url, 
             dataType: dataType,
             callback: _callBack
        });
        packet.done(function(returned_data){
              //this context is a new context, coming from the AJAX constructor
              //response = returned_data; //this is assigning the data too late
              return this.callback(returned_data); 
        });
    }
    //this context wont return anything because its not the AJAX context.
    //return response;
    //response = null;
}

When you create the ajax object, you can assign any kind of arbitrary data in the constructor (JavaScript rules because of this!) - So you define a callback property and assign the _callbackFunction you provided to that property.

In the packet.done context, you call the callback function and pass in the response data.

Your calling method would look like this.

server_request('type','url','datatype','data', 
      function(returnedData){ 
           //now you can work with the returend data in the correct context.
       });
Piotr Kula
  • 9,597
  • 8
  • 59
  • 85