1.加载请求 2.错误处理 3.请求全局事件
在 Ajax 课程中,我们了解了最基本的异步处理方式。本章,我们将了解一下 Ajax 的 一些全局请求事件、跨域处理和其他一些问题。
在 Ajax 异步发送请求时,遇到网速较慢的情况,就会出现请求时间较长的问题。而超 过一定时间的请求,用户就会变得不再耐烦而关闭页面。而如果在请求期间能给用户一些提 示,比如:正在努力加载中…,那么相同的请求时间会让用户体验更加的好一些。
jQuery 提供了两个全局事件,.ajaxStart()和.ajaxStop()。这两个全局事件,只要用户触发 了 Ajax,请求开始时(未完成其他请求)激活.ajaxStart(),请求结束时(所有请求都结束了) 激活.ajaxStop()。
//请求加载提示的显示和隐藏 $('.loading').ajaxStart(function () { $(this).show(); }).ajaxStop(function () { $(this).hide(); }); 12345 12345注意:以上代码在 jQuery1.8 及以后的版本不在有效,需要使用 jquery-migrate 向下兼容 才能运行。新版本中,必须绑定在 document 元素上。
$(document).ajaxStart(function () { $('.loading').show(); }).ajaxStop(function () { $('.loading').hide(); }); //如果请求时间太长,可以设置超时 $.ajax({ timeout : 500 }); //如果某个 ajax 不想触发全局事件,可以设置取消 $.ajax({ global : false }); 123456789101112131415 123456789101112131415Ajax 异步提交时,不可能所有情况都是成功完成的,也有因为代码异步文件错误、网 络错误导致提交失败的。这时,我们应该把错误报告出来,提醒用户重新提交或提示开发者 进行修补。 在之前高层封装中是没有回调错误处理的,比如 .get()、 .post()和.load()。所以,早期 的方法通过全局.ajaxError()事件方法来返回错误信息。而在 jQuery1.5 之后,可以通过连缀 处理使用局部.error()方法即可。而对于$.ajax()方法,不但可以用这两种方法,还有自己的属 性方法 error : function () {}。
//$.ajax()使用属性提示错误 $.ajax({ type : 'POST', url : 'test1.php', data : $('form').serialize(), success : function (response, status, xhr) { $('#box').html(response); }, error : function (xhr, errorText, errorStatus) { alert(xhr.status + ':' + xhr.statusText); } }); //$.post()使用连缀.error()方法提示错误,连缀方法将被.fail()取代 $.post('test1.php').error(function (xhr, status, info) { alert(xhr.status + ':' +xhr.statusText); alert(status + ':' + info); }); //$.post()使用全局.ajaxError()事件提示错误 $(document).ajaxError(function (event, xhr, settings, infoError) { alert(xhr.status + ':' +xhr.statusText); alert(settings+ ':' + info); }); 12345678910111213141516 12345678910111213141516jQuery 对于 Ajax 操作提供了很多全局事件方法,.ajaxStart()、.ajaxStop()、.ajaxError() 等事件方法。他们都属于请求时触发的全局事件,除了这些,还有一些其他全局事件: .ajaxSuccess(),对应一个局部方法:.success(),请求成功完成时执行。 .ajaxComplete(),对应一个局部方法:.complete(),请求完成后注册一个回调函数。 .ajaxSend(),没有对应的局部方法,只有属性 beforeSend,请求发送之前要绑定的函数。
//$.post()使用局部方法.success() $.post('test.php', $('form').serialize(), function (response, status, xhr) { $('#box').html(response); }).success(function (response, status, xhr) { alert(response); }); //$.post()使用全局事件方法.ajaxSuccess() $(document).ajaxSuccess(function (event, xhr, settings) { alert(xhr.responseText); }); 12345678910 12345678910注意:全局事件方法是所有 Ajax 请求都会触发到,并且只能绑定在 document 上。而局 部方法,则针对某个 Ajax。 对于一些全局事件方法的参数,大部分为对象,而这些对象有哪些属性或方法能调用,可以通过遍历方法得到。
//遍历 settings 对象的属性 $(document).ajaxSuccess(function (event, xhr, settings) { for (var i in settings) { alert(i); } }); //$.post()请求完成的局部方法.complete() $.post('test.php', $('form').serialize(), function (response, status, xhr) { alert('成功'); }).complete(function (xhr,status) { alert('完成'); }); //$.post()请求完成的全局方法.ajaxComplete() $(document).ajaxComplete(function (event, xhr, settings) { alert('完成'); }); //$.post()请求发送之前的全局方法.ajaxSend() $(document).ajaxSend(function (event, xhr, settings) { alert('发送请求之前'); }); //$.ajax()方法,可以直接通过属性设置即可。 $.ajax({type : 'POST', url : 'test.php', data : $('form').serialize(), success : function (response, status, xhr) { $('#box').html(response); }, complete : function (xhr, status) { alert('完成' + ' - ' + xhr.responseText + ' - ' + status); }, beforeSend : function (xhr, settings) { alert('请求之前' + ' - ' + xhr.readyState + ' - ' + settings.url); } }); 1234567891011121314151617181920212223242526272829303132 1234567891011121314151617181920212223242526272829303132注意:在 jQuery1.5 版本以后,使用.success()、.error()和.complete()连缀的方法,可以 用.done()、.fail()和.always()取代。