0

I have a function x1 that has a ajax call to the server something like below

var result;

function x1()
{
    $.ajax({
        type: Type, //GET or POST or PUT or DELETE verb
        url: Url, // Location of the service        
        contentType: ContentType, // content type sent to server
        dataType: DataType, //Expected data format from server        
        success: function (msg) {//On Successfull service call
            result = msg.GetUrlContentResult; 
        },
        error: function (xhr, ajaxOptions, thrownError) {

        }
    });
}

I have another function x11 that calls x1 that depends on this value of variable result which is global var.

function x11()
{
    x1();
    if (result==something)
    {do something}
}

problem is since x1() is async function the result is not set when the if result get's executed. i think i have to do some type of callback, looking at some examples for callback i am little new to this any help how to correctly setup a callback so value of result is set when it returns from x1? i have more than one function that calls x1()

Justin Homes
  • 3,739
  • 9
  • 49
  • 78
  • What if you put the "if (result==something) {do something}" part within the success callback function? You could also add it to the error callback function if essential. – HartleySan Apr 30 '13 at 00:35
  • i have more than one function that calls x1() and process the result diffrently – Justin Homes Apr 30 '13 at 00:38

2 Answers2

3

A single ajax call can have multiple success handlers. Return the jqXHR object from x1() and bind an additional success handler to it from x11() using the .done() method:

function x1()
{
    return $.ajax({
        ...
    });
}
function x11()
{
    x1().done(function(msg) {
        // this is an additional success handler
        // both handlers will be called
    });
}
gilly3
  • 87,962
  • 25
  • 144
  • 176
  • do i need to add the return next to $.ajax? – Justin Homes Apr 30 '13 at 00:40
  • @JustinHomes - Yes. Otherwise the `x1()` returns `undefined` and you'll get a runtime error when you try to call `x1().success()`. There is no method "success" of undefined. – gilly3 Apr 30 '13 at 00:42
  • @JustinHomes - If your real function doesn't actually exit immediately after making the ajax call, you can store the `jqXHR` object in a variable and return the variable at the end of the function. – gilly3 Apr 30 '13 at 00:45
  • The `.success()` method is deprecated in favor of `.done()` – Ian Apr 30 '13 at 02:22
2

What you're trying to do is actually something as simple as this: (edited for dynamic callback)

function x1(callback)
{
    $.ajax({
        type: Type, //GET or POST or PUT or DELETE verb
        url: Url, // Location of the service        
        contentType: ContentType, // content type sent to server
        dataType: DataType, //Expected data format from server        
        success: callback,
        error: function (xhr, ajaxOptions, thrownError) {

        }
    });
}

x1(x11);

You'll have to modify the x11 function to accept the msg argument and update the result variable:

function x11(msg) {
    result = msg.GetUrlContentResult;
    //...
}
Rick Viscomi
  • 8,180
  • 4
  • 35
  • 50