开发者

Extjs, return Ajax return value

I have a problem with using Ajax.

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

The problem is, before get the ajax开发者_Go百科 response, it return cnt. so it always return NULL.

is there a way to make right return response value?

Thanks you!


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.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜