2

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!

1 Answer 1

6

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);
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your reply, I know it's because asynchronous but I have many time this problem while working, So I'd like to know how expert do in this case...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.