开发者

get the response of a jquery ajax call as an input parameter of another function:

开发者 https://www.devze.com 2023-02-09 00:27 出处:网络
Is it possible to make a jQuery ajax call, and get the response as an input parameter of another function?

Is it possible to make a jQuery ajax call, and get the response as an input parameter of another function?

Here is an example, I开发者_Go百科 have the following function call at a location:

updateTips(validationTextObject,objUsers.fetchAvailable());

the objUsers.fetchAvailable() function makes a ajax call to the server. The callback function on successful call would be something like this. It is being used to process the result

BHUsers.prototype.recvAvailable= function(response){
    // some kind of processing over here
    return (response["status"] == "OK")? "Available" : "Not Available";
}

I want that function to return the processed result which can be used as a parameter to the function updateTips

The primary goal of this is to be able to do all of this for multiple scenarios, rather than writing multiple functions for the same call. Also I want the calling and the response processing functions to just do what they are doing. I don't want to add html objects into it.

Any Clues?


assuming objUsers.fetchAvailable's prototype sends an ajax request using jquery.

You would set the success function like this:

BHUsers.prototype.fetchAvailable = function(){
    $.ajax({
        url:'', 
        success: this.recvAvailable // bind success to `recvAvailable`
    })
}

But since the ajax success call back has data, textStatus, XHR parameters, you could wrap it with an anonymous function.

BHUsers.prototype.fetchAvailable = function(){
    var self = this;
    $.ajax({
        url:'', 
        success: function(data, textStatus, XHR){
            self.recvAvailable(XHR); // bind success to `recvAvailable`
        }
    })
}
0

精彩评论

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