加载请求: .ajaxStart() 和 .ajaxstop()

    $(document).ajaxStart(function(){
$('.loading').show();
}).ajaxStop(function(){
$('.loading').hide();
});

错误处理: .ajaxError()

    //1 $.ajax()使用属性提示错误
$('form input[type=button]').click(function(){
$.ajax({
type:"post",
url:"test1.php",
async:true,
data:$('form').serialize(),
success:function(response,status,xhr){
$('#box').html(response);
},
// timeout:3000
// global:false
error:function(xhr,errorText,errorType){
// alert(errorText + ':' +errorType);
alert(xhr.status + ':' +xhr.statusText);
} });
}); //2 $.post()使用连缀.error()方法提示错误,将被.fail()取代
$('form input[type=button]').click(function(){
$.post('test1.php').error(function(xhr,errorText,errorType){
alert(errorText + ':' +errorType);
alert(xhr.status + ':' +xhr.statusText);
});
}); //3 使用全局.ajaxError()方法
$(document).ajaxError(function(event,xhr,settings,info){
alert(event.type);
alert(event.target);
for(var i in event){ //打印出event的所有属性
document.write(i + '<br />');
}
});

.ajaxSuccess(),对应一个局部方法:.success(),请求成功完成时执行。

.ajaxComplete(),对应一个局部方法:.complete(),请求完成后注册一个回调函数。

.ajaxSend(),没有对应的局部方法,只有属性 beforeSend,请求发送之前要绑定的函数。

    //$.post()使用全局
$('form input[type=button]').click(function(){
$.post('test.php',$('form').serialize());
}); $('document').ajaxSend(function(){
alert(发送请求之前执行);
}).ajaxComplete(function(response,status,xhr){
alert('请求完成后,不管是否失败成功');
}).ajaxSuccess(function(event,xhr,settrings){
alert('请求成功后,不管是否成功');
}).ajaxError(function(event,xhr,settrings){
alert('请求失败后');
}); //$.post()使用局部
$('form input[type=button]').click(function(){
$.post('test.php',$('form').serialize()).success(function(){
alert('请求成功后');
}).complete(function(){
alert('请求完成后');
}).error(function(){
alert('请求失败后');
});
}); //$.ajax()使用属性
$('form input[type=button]').click(function(){
$.ajax({
type:"post",
url:"test1.php",
async:true,
data:$('form').serialize(),
success:function(response,status,xhr){
alert('请求成功后');
},
complete:function(){
alert('请求完成后,不管失败成功');
},
beforeSend:function(){
alert('发送请求之前');
},
error:function(xhr,errorText,errorType){
alert('请求失败后');
}
});
});

注:

jQuery1.5 版本以后,使用.success().error().complete()连缀的方法,可以用.done().fail().always()取代。

Ajax :六个全局事件的更多相关文章

  1. jquery的ajax全局事件详解

        jquery在ajax方面是非常强大和方便的,以下是jquery进行ajax请求时方法模板: $.ajax({ type: "get", url: "" ...

  2. 关于数据未渲染完,要获取document高度问题——ajax全局事件

    昨天在做开发时,遇到这样一个问题,当页面刚加载的时候,就要获取document的高度,可是此时页面上所有的ajax请求的数据都还没有渲染到页面上,所以导致得到的document的高度仅仅是页面结构的高 ...

  3. Jquery表单序列化和AJAX全局事件

    Jquery表单序列化 1.必须放在form标签内: 2.控件必须有name属性: 3.控件的value值会提交到服务器: 如: <form id="form1"> & ...

  4. 锋利的jQuery读书笔记---jQuery中Ajax--序列化元素、Ajax全局事件

    serialize()方法: 作用于一个jQuery对象,它能够将DOM元素内容序列化为字符串,用于Ajax请求. <!DOCTYPE html> <html> <hea ...

  5. ajax全局事件

    作用:当你的页面存在很多ajax事件的话,我们有一些信息是公共的,可以复用,我们可以用全局事件进行编写,因为每一个ajax事件调用,都会触发ajax全局事件. jquery的ajax方法的全部全局事件 ...

  6. jquery ajax 全局事件

    jquery的ajax方法的全部全局事件:(不管是$.ajax().$.get().$.load().$.getJSON()等都会默认触发全局事件) ajaxStart:ajax请求开始前 ajaxS ...

  7. jQuery中的Ajax全局事件

    Ajax全局事件 全局事件会在有ajax请求的情况下触发. 方法名称 说明 ajaxStart(callback) Ajax请求开始时执行的函数 ajaxStop(callback) Ajax请求结束 ...

  8. nprogress进度条和ajax全局事件

    nprogress和ajax全局事件 nprogress 官方网站:http://ricostacruz.com/nprogress/ 下载地址:https://github.com/rstacruz ...

  9. 【锋利的jQuery】中全局事件ajaxStart、ajaxStop不执行

    最近一直都在研究[锋利的jQuery],确实是一本好书,受益匪浅.但由于技术发展及版本更新等原因,里面还是有些坑需要踩的. 比如:第六章七节中提到的全局事件ajaxStart.ajaxStop照着案例 ...

随机推荐

  1. linux 下Redis 5.0主从复制(一主二从)哨兵模式的搭建

    文档结构如下: 一.环境说明: 作用 IP地址 端口 操作系统版本 安装目录 哨兵文件 主库 172.16.10.80 6379 Redhat 6.7 /redis5.0/redis-5.0.0 Se ...

  2. Python中functools模块函数解析

    Python自带的 functools 模块提供了一些常用的高阶函数,也就是用于处理其它函数的特殊函数.换言之,就是能使用该模块对可调用对象进行处理. functools模块函数概览 functool ...

  3. ffmpeg x264编译与使用介绍

    问题1:我用的是最新版本的ffmpeg和x264,刚刚编译出来,编译没有问题,但是在linux 环境使用ffmpeg的库时发现报错error C3861: 'UINT64_C': identifier ...

  4. 广播Intent的三种方式总结

    1.android有序广播和无序广播的区别 BroadcastReceiver所对应的广播分两类:普通广播和有序广播. 普通广播通过Context.sendBroadcast()方法来发送.它是完全异 ...

  5. vue入门--简单嵌套路由的一个路径小问题

    假设现在有一个项目,刚进去要显示main页面下的contorl页面,那么路由里面的初级路由应该是{main和err},这两个是同一级,然后{control和set}是main下的子路由,foot是这两 ...

  6. 转:Hibernate使用SQLQuery

    原文:http://hi.baidu.com/luo_qing_long/blog/item/783a15eceb75abdd2f2e21b0.html 对原生SQL查询执行的控制是通过SQLQuer ...

  7. UTC时间和各个地区的时间到底是怎么回事

    就不分析了,直接写结论 1.同一个时间点全球各地的时间戳是一致的 2.同一个时间戳在不同的时区对应不同的时间   依北京时间为例: 当前时间为 Tue Jan 23 2018 19:02:11 GMT ...

  8. 算法21----重塑矩阵 LeetCode566

    1.题目 在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据. 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重 ...

  9. 2019-02-25 SQL:cast(itemvalue as decimal(19,4))

    1.Operand data type nvarchar(max) is invalid for sum operator 要转换格式 2.Conversion failed when convert ...

  10. HDU 4324 Contest 3

    直接DFS即可 #include <iostream> #include <string.h> #include <algorithm> #include < ...