当AJAX调用正在进行时显示“加载”图像

我有以下jQuery Ajax调用:

$.ajax({ type: "POST", url: "addtobasket.php", data: "sid=&itemid=" + itemid + "&boxsize=" + boxsize + "&ext=" + extraval, success: function(msg){ $.post("preorderbasket.php", { sid: "", type: "pre" }, function(data){ $('.preorder').empty(); $('.preorder').append(data); }); } }); 

我想在ajax调用进行时显示图像。 我怎样才能做到这一点?

谢谢,

试试这个 :

  
Loading....

这将显示每次执行ajax请求时的加载图像,我已经在页面顶部实现了这个div,因此它不会阻碍页面,但是你总能看到ajax调用何时进行。

沿着这些方向的东西( showLoadingImagehideLoadingImage由你决定):

 // show the loading image before calling ajax. showLoadingImage(); $.ajax({ // url, type, etc. go here success: function() { // handle success. this only fires if the call was successful. }, error: function() { // handle error. this only fires if the call was unsuccessful. }, complete: function() { // no matter the result, complete will fire, so it's a good place // to do the non-conditional stuff, like hiding a loading image. hideLoadingImage(); } }); 

您可以在调用$ .ajax()之前显示图像,然后在post handler函数中隐藏/删除图像(就在.empty()/。append(data)调用之前)。