jQuery的ajax调用不设置变量的值 [英] jQuery ajax call not setting variable's value

查看:177
本文介绍了jQuery的ajax调用不设置变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个jQuery调用AJAX,

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;
        };

现在的问题是第一个警报显示 2.873 (在其他情况下,它显示了 2.178 ),但问题是第二个警告,第二次警报显示,来到这里,没有定义 ??? FTW?什么是黑客是怎么了?我一直在瞎搞这2天,但没有!

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!

为什么 _valToReturn 的价值不确定的第二个警告?并确定在第一?任何帮助吗?

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

编辑: 警报来到这里_valToReturn 第一个警报后执行。所以,(差不多了!),所有的回答者表示,如果来之前,这将是不确定的,我知道,但我说这是执行不前。

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.

推荐答案

Ajax是一种异步请求。通过成功的事件执行的时间,警报来这里未定义将执行。和可变没有被初始化。

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.

更新: 在回答你的问题密切关注,我相信 匿名函数认为valToReturn作为一个局部变量。因此,这些变量在不同的堆栈如果你愿意。

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.

请参阅此: <一href="http://stackoverflow.com/questions/6153666/javascript-anonymous-function-not-updating-global-variable">Javascript匿名函数没有更新全局变量

<一个href="http://stackoverflow.com/questions/4307819/using-variable-outside-of-ajax-callback-function">Using Ajax回调函数外部变量

类似你。

这篇关于jQuery的ajax调用不设置变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆