Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `success: function(msg)` mean in my jQuery Ajax call?

Tags:

jquery

ajax

I have two jQuery Ajax calls that I'm combining on a page. I'm stuck on the success: function() in each, as one is success: function(msg) and the other is success: function(data). I'm unsure of what both of these mean, and what they should be in the combined code. I'll place the two calls below, separately, and combined as I have them thus far.

Ajax Request #1: there is a $msg .= "<div class='pagination'><ul>"; on this functions php page. Not sure if that is what this is referring to.

$.ajax
({
    type: "GET",
    url: "new_arrivals_data.php",
    data: "page="+page,
    success: function(msg)
    {
        $("#gallery_container").ajaxComplete(function(event, request, settings)
        {
            gallery_show();
            loading_hide();
            $("#gallery_container").html(msg);
        });
    }
});

Ajax Request #2: As far as I can see, there is no data anywhere on this call's php file. Don't know what function(data) is referring to.

$.get("new_arrivals_data.php",{imgs: value}, function(data){
    $("#gallery_container").html(data);
});

Combined Request: I have put a ? where msg was in the original call as I'm unsure what to put in it's spot.

$.ajax
({
    type: "GET",
    url: "new_arrivals_data.php",
    data: {page:page, imgs: value},
    success: function(?)
    {
        $("#gallery_container").ajaxComplete(function(event, request, settings)
        {
            gallery_show();
            loading_hide();
            $("#gallery_container").html(?);
        });
    }
});
like image 822
stefmikhail Avatar asked Aug 21 '11 01:08

stefmikhail


1 Answers

msg and data are simply the names of the formal parameters. You use those to refer to the response data that is passed to that function when it is invoked.

You can rename it to any valid JavaScript identifier.

Although there isn't really any reason to call ajaxComplete inside the success: callback:

success: function( whatever_you_want_to_call_it ) {
        gallery_show();
        loading_hide();
        $("#gallery_container").html( whatever_you_want_to_call_it );
}

$.get("new_arrivals_data.php",{imgs: value}, function( i_like_ice_cream ){
    $("#gallery_container").html( i_like_ice_cream );
});

Remember, in both cases, you're passing a function as an argument. That function is invoked when the response is received.

Whatever code is invoking that function, is also passing the response into that function as the first argument so that you have access to it. That's why you defined the parameter.

It's very similar to declaring a variable in a function.

$.get("new_arrivals_data.php",{imgs: value}, function(){

    var i_like_ice_cream = arguments[0];
    $("#gallery_container").html( i_like_ice_cream );

});

This does almost the same thing. You've associated a variable with the first argument passed into your callback function.

like image 59
user113716 Avatar answered Sep 29 '22 06:09

user113716