Abhijit
Abhijit

Reputation: 49

Return bool value from Ajax

I am calling the below function from my .aspx page and all I want to check whether this function returned true or false. I tried many things but I get undefined as result.

I am calling function using below code

 if (IsIncetiveAllowed())
    {
        sCondition = ".//LISTENTRY[VALUEID='" + m_sIncentiveReleaseId + "']";
        xmlNode = $(XMLCombos).xpath(sCondition)[0];
        XMLCombos.firstChild.removeChild(xmlNode);
    }

function IsIncetiveAllowed() {
$.ajax({
        cache: false,
        async: false,
        type: "POST",
        url: "pp060.aspx/CheckIncentiveAllowed",
        data: "{'typeOfApplication': '" + m_TypeOfMortgage + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {

            if (response.d)
                return true;
            else
                return false;
        },
        error: function (response) {
            MessageBox.Show("An error occurred checking IsIncetiveAllowed method.", null, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    });
}

Please Help!

Upvotes: 1

Views: 57

Answers (3)

Aditya Kumar Singh
Aditya Kumar Singh

Reputation: 3

Well this is happening because the ajax call is asynchronous. You can put your code present in if block to the ajax callback function to implement your logic

Upvotes: 0

Tomalak
Tomalak

Reputation: 338158

First off, you never want to use synchronous Ajax. Synchronous Ajax blocks the browser, the user interface freezes and the user cannot scroll, click or do or anything while synchronous requests load. Don't use them.

Second, it's useful to break up your operation into separate parts. What you have here is

  1. A part can post JSON to the server
    This is the most re-usable part, it works the same for all JSON you want to post to any URL.
  2. A part that knows how to talk to to a specific endpoint on the server
    This is the second most reusable part, it can send any data to a specific endpoint.
  3. A part that uses this endpoint This is the least reusable part, it can send specific data to a specific endpoint.

It makes sense to have a separate function for each part. jQuery supports this easily, because all Ajax methods return promises, and promises can be given from function to function.

Part 1, as a jQuery extension for maximum re-usability:

$.fn.postJSON = function(url, data) {
    return $.ajax({
        type: "POST",
        url: url,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(data)
    });
};

Part 2, as a stand-alone function. Note that I am matching the remote API endpoint name. You can write more functions like this to wrap other API endpoints.

function checkIncentiveAllowed(typeOfApp) {
    return $.postJSON("pp060.aspx/CheckIncentiveAllowed", {
        typeOfApplication: typeOfApp
    }).fail(function (err) {
        MessageBox.Show("An error occurred in checkIncentiveAllowed method.", 
            null, MessageBoxButtons.OK, MessageBoxIcon.Error);
        console.log(err);
    });
}

Part 3, to be used inside an event handler for example:

checkIncentiveAllowed(m_TypeOfMortgage).done(function (response) {
    var path = ".//LISTENTRY[VALUEID='" + m_sIncentiveReleaseId + "']",
        xmlNode = $(XMLCombos).xpath(path)[0];

    if (response.d && xmlNode) {
        xmlNode.parentNode.removeChild(xmlNode);
    } else {
        // not allowed
    }
});

Upvotes: 0

S. Dev
S. Dev

Reputation: 619

If you pass a callback to the IsIncetiveAllowed function, you can make it execute your code with the result of the ajax call after it has been made.

IsIncetiveAllowed(function(is_allowed) {
    if (is_allowed) {
        sCondition = ".//LISTENTRY[VALUEID='" + m_sIncentiveReleaseId + "']";
        xmlNode = $(XMLCombos).xpath(sCondition)[0];
        XMLCombos.firstChild.removeChild(xmlNode);
    }
    else {
        // Not allowed
    }
});

function IsIncetiveAllowed(callback) {
    $.ajax({
        cache: false,
        async: false,
        type: "POST",
        url: "pp060.aspx/CheckIncentiveAllowed",
        data: "{'typeOfApplication': '" + m_TypeOfMortgage + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            if (response.d)
                callback(true);
            else
                callback(false);
        },
        error: function (response) {
            MessageBox.Show("An error occurred checking IsIncetiveAllowed method.", null, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    });
}

Upvotes: 1

Related Questions