Expert wanna be
Expert wanna be

Reputation: 10624

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 response, it return cnt. so it always return NULL.

is there a way to make right return response value?

Thanks you!

Upvotes: 2

Views: 6053

Answers (1)

hspain
hspain

Reputation: 17568

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

Upvotes: 6

Related Questions