开发者

Combining jQuery Ajax Requests

I have two Ajax requests that I am wanting to combine together into one. I'm having trouble figuring how to do this as one is using $.ajax() and another is using $.get().

As I'm fairly new to Ajax, this is causing me much pain. If you could help, it would me much appreciated.

Ajax Request #1

$.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);
开发者_如何学C        });
    }
});

Ajax Request #2

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

Thanks for any help you can offer.


They are actually both GETs $.get is short hand for $.ajax({ type:'GET'.

So combining them might work depending on your server's response:

$.ajax
({
    type: "GET",
    url: "new_arrivals_data.php",
    data: {page:page, imgs: value},
    success: function(msg)
    {
        gallery_show();
        loading_hide();
        $("#gallery_container").html(msg);
    }
});


Not sure what you're looking for, but Ajax Request #2 is equivalent to:

$.ajax({
    type: 'GET',
    url: "get_images.php",
    data: {imgs: value},
    success: function(data){
        $("#imgTray").html(data);
    }
});

Hope this helps.


You can't just 'combine' requests - you'll still need to make two separate requests to each of these URLs. Since by default, ajax requests are asynchronous, you can fire them both almost simultaneously (if one doesn't depend on the other. As marko points out in the comments, if they are dependent, you could force the requests to be synchronous. $.ajax has an async property.).

if(condition){
    makeRequest1();
    makeRequest2();
}

Also $.get() is simply a convenience method for $.ajax() with certain options preset (such as using GET as the request type).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜