开发者

how to return ajax suceess from user defined function [duplicate]

This question already has answers here: How do I return the response from an asynchronous call? (41 answers) Closed 7 years ago.

I am having the bellow function . Here i want to return ajax success from user defined function . How to do this

alert(Ajaxcall(id_array,"del"));

function Ajaxcall(id_array,type){
            $.ajax({
                    type: "POST",
                    url: "serverpage.php",
                    cache:false,
                    data: ({id:id_array,type开发者_C百科:type}),
                    success: function(msg){

                   return msg; //this returns nothing

                    }
            }); 

            alert(msg); // this one undefined

}

thanks


The "a" in "ajax" stands for "asynchronous" ("Asynchronous JavaScript And XML", although these days most people use it with JSON rather than XML).

So your Ajaxcall function returns before the ajax call completes, which is why you can't return the message as a return value.

The usual thing to do is to pass in a callback instead:

Ajaxcall(id_array,"del", functon(msg) {
    alert(msg);
});

function Ajaxcall(id_array,type, callback){
            $.ajax({
                    type: "POST",
                    url: "serverpage.php",
                    cache:false,
                    data: ({id:id_array,type:type}),
                    success: function(msg){

                        callback(msg);

                    }
            }); 
}

It's surprisingly easy with JavaScript, because JavaScript's functions are closures and can be defined inline. So for instance, suppose you wanted to do this:

function foo() {
    var ajaxStuff, localData;

    localData = doSomething();
    ajaxStuff = getAjaxStuff();
    doSomethingElse(ajaxStuff);
    doAnotherThing(localData);
}

you can literally rewrite that asynchronously like this:

function foo() {
    var localData;

    localData = doSomething();
    getAjaxStuff(function(ajaxStuff) {
        doSomethingElse(ajaxStuff);
        doAnotherThing(localData);
    });
}

I should note that it's possible to make an ajax call synchronous. In jQuery, you do that by passing the async option in (setting it false). But it's a very bad idea. Synchronous ajax calls lock up the UI of most browsers in a very user-unfriendly fashion. Instead, restructure your code slightly as above.

But just for completeness:

alert(Ajaxcall(id_array,"del"));

function Ajaxcall(id_array,type){
    var returnValue;
            $.ajax({
                    type: "POST",
                    url: "serverpage.php",
                    cache:false,
                    async: false,        // <== Synchronous request, very bad idea
                    data: ({id:id_array,type:type}),
                    success: function(msg){

                        returnValue = msg;

                    }
            }); 
    return returnValue;
}


JQuery has a number of global Ajax event handlers, including $.ajaxComplete() and $.ajaxSuccess() (ref: http://api.jquery.com/ajaxSuccess/).

The code can be attached to any DOM element and looks like this:

/* Gets called when all request completes successfully */
      $("#myElement").ajaxSuccess(function(event,request,settings){
          $(this).html("<h4>Successful ajax request</h4>");
      });

This code will execute whenever any successful Ajax call is completed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜