jQuery学习笔记之十三------Ajax进阶

一  加载请求

$('.loading').ajaxStart(function () {
$(this).show();
}).ajaxStop(function () {
$(this).hide();
});
注意:以上代码在 jQuery1.8 及以后的版本不在有效,需要使用 jquery-migrate 向下兼容
才能运行。新版本中,必须绑定在 document 元素上。
$(document).ajaxStart(function () {
$('.loading').show();
}).ajaxStop(function () {
$('.loading').hide();
});


二  错误处理

//$.ajax()使用属性提示错误
$.ajax({
type : 'POST',
url : 'test1.php',
data : $('form').serialize(),
success : function (response, status, xhr) {
$('#box').html(response);
},
error : function (xhr, ) {
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);
});

三  请求全局事件

//$.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);
});



//遍历 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);
}
});

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

四.JSON 和 JSONP


如果在同一个域下,$.ajax()方法只要设置 dataType 属性即可加载 JSON 文件。而在非
同域下,可以使用 JSONP,但也是有条件的。
//$.ajax()加载 JSON 文件
$.ajax({
type : 'POST',
url : 'test.json',
dataType : 'json',
success : function (response, status, xhr) {
alert(response[0].url);
}
});
如果想跨域操作文件的话,我们就必须使用 JSONP。JSONP(JSON with Padding)是一个
非官方的协议,它允许在服务器端集成 Script tags 返回至客户端,通过 javascript callback 的
形式实现跨域访问(这仅仅是 JSONP 简单的实现形式)。
//跨域的 PHP 端文件
<?php
$arr = array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
$result = json_encode($arr);
$callback = $_GET['callback'];
echo $callback."($result)";
?>
//$.getJSON()方法跨域获取 JSON
$.getJSON('http://www.li.cc/test.php?callback=?', function (response) {
console.log(response);
});
//$.ajax()方法跨域获取 JSON
$.ajax({
url : 'http://www.li.cc/test.php?callback=?',
dataType : 'jsonp',
success : function (response, status, xhr) {
console.log(response);
alert(response.a);
}
});
注意:这里的 URL 如果不想后面跟着?callback=?,那么可以给$.ajax()方法增加一
个属性即可。
//使用 jsonp 属性
$.ajax({
url : 'http://www.li.cc/test.php',
jsonp : 'callback'
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值