Extjs,返回 Ajax 返回值 [英] Extjs, return Ajax return value

查看:37
本文介绍了Extjs,返回 Ajax 返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Ajax 时遇到问题.

I have a problem with using Ajax.

function GetGrantAmazonItemCnt(){
    var cnt;
    Ext.Ajax.request({
        url : '',
        params : {},
        success :function(response){
            cnt = response.responseText;
        }
    });
    return cnt; 
}

问题是,在得到ajax响应之前,它返回了cnt.所以它总是返回NULL.

The problem is, before get the ajax response, it return cnt. so it always return NULL.

有没有办法制作正确的返回响应值?

is there a way to make right return response value?

谢谢!

推荐答案

因为 AJAX 请求是异步的,所以您的 cnt 变量将在请求返回并调用成功处理程序之前返回.

Because the AJAX request is asynchronous, your cnt variable will return before the request comes back and the success handler is called.

我建议重构您的代码以解决这个问题.

I would suggest refactoring your code to account for this.

执行此操作的一种方法是从 AJAX 请求的成功处理程序调用调用 GetGrantAmazonItemCnt() 的任何函数,通过这种方式将值传递到需要去的地方:

One way to do this is to call whichever function that called GetGrantAmazonItemCnt() from the success handler of your AJAX request, this way passing the value to where it needs to go:

function GetGrantAmazonItemCnt(){
    var cnt;
    Ext.Ajax.request({
        url : '',
        params : {},
        success :function(response){
            cnt = response.responseText;
            FunctionThatCalledMe(cnt);
        }
    });
}

这篇关于Extjs,返回 Ajax 返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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