3

I wrote this script FUNCTION to check whether the petty cash is already 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 the JS script always returns undefined... Any suggestion on how to fix this?

I tried testing it:

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

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

alert(data);
//the Result: [object Object]
3
  • do an alert(data) inside success: function (data){ ans see what u get. Can u share that? do you get any data? Commented Jun 30, 2015 at 1:57
  • Is login.CashInAmount defined at CashSetAmount = login.CashInAmount ? Commented Jun 30, 2015 at 2:08
  • Nope...login.CashInAmount is defined at login.CheckPettyCash(); We've been using DLL Library Commented Jun 30, 2015 at 3:49

1 Answer 1

1

$.ajax does not wait for the AJAX request to return. It instead starts the request and continues. If you changed your function to

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

It would always return true. The return values you are specifying are the return values for the anonymous callback functions that you defined with the function keyword.

You need to use the callback functions to notify your page of incoming data. You do not want to write a function that causes the whole page to wait until a response is received.

For example, you could 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)
           // ...
    }
Sign up to request clarification or add additional context in comments.

4 Comments

by the way.. is it possible to wait for the AJAX like $(AJAX).ready()?
Like I said, you most likely do not want a function to wait like that because the entire page will wait until the function returns.
is a Boostrap Modal dialog that indicates "Please Wait..." recommended? I'll use the callback to close the Modal if it is done.
I guess it depends on context, but I would try to avoid it. In general I think users expect everything to happen silently in the background, but if you click something that the user might expect to happen instantly, it would make sense to give some kind of a status message either just on the page, or as a modal dialog.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.