Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在.ajaxStart()中使用$.ajax_Jquery_Ajax - Fatal编程技术网

Jquery 在.ajaxStart()中使用$.ajax

Jquery 在.ajaxStart()中使用$.ajax,jquery,ajax,Jquery,Ajax,我从API得到一些数据,数据很大。我通过AJAX调用获取这些数据 我的问题是,我想显示一个加载器图像,直到所有数据都被提取出来。在AJAX成功函数中,我隐藏了加载程序图像 这并不能解决我的问题:图像是隐藏的,但数据仍在提取中 我在谷歌上搜索,找到了.ajaxStart()和.ajaxStop(),但是.ajaxStart()中没有关于如何使用AJAX的更多信息 $(document).ajaxStart(function(){ $.ajax(); // my ajax call }); $

我从API得到一些数据,数据很大。我通过AJAX调用获取这些数据

我的问题是,我想显示一个加载器图像,直到所有数据都被提取出来。在AJAX成功函数中,我隐藏了加载程序图像

这并不能解决我的问题:图像是隐藏的,但数据仍在提取中

我在谷歌上搜索,找到了
.ajaxStart()
.ajaxStop()
,但是
.ajaxStart()
中没有关于如何使用AJAX的更多信息

$(document).ajaxStart(function(){
  $.ajax(); // my ajax call
});

$(document).ajaxStart(function(){
    image.hide(); // hide my image
 });
但这并不能解决我的问题。有人能告诉我怎么做吗?

写你的
$.ajax()并将以下内容作为全局代码保存

$(document).ajaxStart(function() {
    $('img').show();
}).ajaxStop(function() {
    $('img').hide();
});
如果您仍然发现任何困难,请尝试在
回调中执行此操作

$.ajax({
   // you ajax attributes 
   beforeSend: function(){
     $("img").show();
   },   
   success: function(data) {
     //handle the data that is being fetched
     $("img").hide();
   },
   error: function(error) {
     //handle the error
     $("img").hide();
   }
 });

如果我正确理解了这个问题,您可能不太清楚
$.ajaxStart()
$.ajaxStop()
是用于什么

如果希望在处理单个Ajax调用时显示加载程序图像,可以执行以下操作:

$('#loader').show();  // assuming the loader is e.g. <div id="loader">loading</div>

$.ajax( ... )         // your ajax call
    .done( function() {
        // when it's finished hide the loader
        $('#loader').hide();
    });

现在我了解了
$.ajaxStart()
$.ajaxStop()
这两个函数的行为。ajaxStop()将运行ajax调用。ajaxyes肯定我想在每个ajax调用上显示加载程序,所以现在我应该在
$.ajaxStart()
之后使用
$.ajaxStop()
对吗?不,在之前。它们设置了事件,您不必用它们包装单个ajax调用。
$( document ).ajaxStart( function() {
    $('#loader').show();
}).ajaxStop( function() {
    $('#loader').hide();
});