Returning a value from a function using AJAX

I wrote this script FUNCTION to check if petty cash is set or not using AJAX ...

function GetPettyCash() {
        $.ajax({
            type: 'GET',
            url: '@Url.Action("CashSet", "POS")',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                if (data["CashSetIn"] == "true") {
                    alert(data["CashSetAmount"]);
                    return true;
                }
                else {
                    alert(data["CashSetIn"]);
                    return false;
                }

            },
            error: function (req, status, errorObj) {
                alert(errorObj.toString());
                return false;
            }
        });
    }

      

I wrote this controller for my Ajax calls:

[HttpGet]
    public JsonResult CashSet()
    {
        Login login = new Login();
        login.CheckPettyCash();
        if (login.CashInSet == true)
        {
            return Json(new
            {
                CashSetIn = "true",
                CashSetAmount = login.CashInAmount

            },JsonRequestBehavior.AllowGet);
        }
        else
        {
            return Json(new
            {
                CashSetIn = "false",
                CashSetAmount = "0"
            }, JsonRequestBehavior.AllowGet);
        }
    }

      

My Controller returns this JSON:

{"CashSetIn":"true","CashSetAmount":1000}

      

But my function in JS script always returns undefined ... Any suggestion on how to fix this?

I tried to test it:

alert(data["CashSetAmount"]);
//the Result: 1000

alert(data["CashSetIn"]);
//the Result: true

alert(data);
//the Result: [object Object]

      

+3


source to share


1 answer


$.ajax

does not expect an AJAX request to return. Instead, it starts the request and continues. If you changed your function to

function GetPettyCash() {
        $.ajax( //...
        );
        return true;
    }

      

It will always return true

. The return values ​​that you specify are the return values ​​for the anonymous callback functions that you have defined with the keyword function

.



You need to use callback functions to notify your page of incoming data. You don't want to write a function that makes the entire page wait until a response is received.

For example, you can do something like this:

function GetPettyCash() {
        $.ajax({
            type: 'GET',
            url: '@Url.Action("CashSet", "POS")',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                if (data["CashSetIn"] == "true") {
                    do_something_with_petty_cash(true);
                }
                else {
                    do_something_with_petty_cash(false);
                }

            },
            error: function (req, status, errorObj) {
                alert(errorObj.toString());
                return false;
            }
        });
    }
function do_something_with_petty_cash(petty_cash) {
       if (petty_cash)
           // ...
    }

      

+1


source







All Articles