Razort4x
Razort4x

Reputation: 3406

jQuery ajax call not setting variable's value

I have this jquery call to ajax,

function findTaxForProcess(argPrc, argPrcAmount, argPrcDiscount) {
            if (argPrc == '') { return 0; };
            var _valToReturn;

            if ($('#hdnTaxBefore').val() == "true") {
                // if tax is calculated before discount
                $.ajax({
                    url: '/AutoComplete.asmx/FindProcessTax',
                    type: 'POST',
                    timeout: 5000,
                    datatype: 'xml',
                    cache: false,
                    data: 'argProcess=' + argPrc + '&argAmt=' + argPrcAmount,
                    success: function (response) {
                        _valToReturn = $(response).find('double').text();
                        alert(_valToReturn);
                    }
                });
            }

            else {
                // the tax is calculated after discount
                $.ajax({
                    url: '/AutoComplete.asmx/FindProcessTaxAter',
                    type: 'POST',
                    timeout: 5000,
                    datatype: 'xml',
                    cache: false,
                    data: 'argProcess=' + argPrc + '&argAmt=' + argPrcAmount + '&argDiscount=' + argPrcDiscount,
                    success: function (response) {
                        _valToReturn = $(response).find('double').text();
                        alert(_valToReturn);
                    }
                });
            }
            alert('came here ' + _valToReturn);
            return _valToReturn;
        };

The problem is first alert shows 2.873 (in the else case, it shows 2.178), but the problem is second alert, the second alert shows , came here undefined??? FTW? what the hack is going wrong? I've been messing around this for 2 days but nothing!

Why is the value of _valToReturn undefined at second alert? and defined at first? any help?

EDIT : The alert came here _valToReturn is executing after the first alert. So, as (almost!) all answerer said that if its coming before, it would be undefined, I know that, but as I said it is executing after not before.

Upvotes: 0

Views: 1812

Answers (4)

Lews Therin
Lews Therin

Reputation: 10995

Ajax is an asynchronous request. By the time the success event executes, the alert "came here undefined" would have executed. And the variable has not been initialized by then.

Update: In answer to your question looking closely, I believe The anonymous function sees valToReturn as a local variable. Hence those variables are on a different "stack" if you will.

See this: Javascript anonymous function not updating global variable

Or

Using variable outside of ajax callback function

Similar to yours.

Upvotes: 2

xiaohao
xiaohao

Reputation: 272

you can try to use console.log to find which is executing first, in my mind, the alert with 'came here ' should be executing first, just go and have a look at firebug or chrome console

Upvotes: 0

AboQutiesh
AboQutiesh

Reputation: 1716

try to initialize the _valToReturn value to get rid of undefined

Upvotes: 0

mohanrajt
mohanrajt

Reputation: 152

If the second alert comes first, that is if undefined comes first and then the value, then it is because of the async call of ajax. To overcome this you can make the ajax call synchronous.

If the second alert comes after the first one then it may the problem with the variable value.

Upvotes: 0

Related Questions