zepto ajax php实例,Zepto Ajax 事件

本文详细介绍了Ajax请求的生命周期内触发的事件,包括ajaxStart、ajaxBeforeSend、ajaxSend、ajaxSuccess、ajaxError、ajaxComplete和ajaxStop。每个事件的作用和触发条件都进行了阐述,同时提及了事件在不同上下文中的触发方式。示例代码展示了如何监听和使用这些事件,帮助读者深入理解Ajax请求的过程。
摘要由CSDN通过智能技术生成

当global: true时。在Ajax请求生命周期内,以下这些事件将被触发。

ajaxStart (global):如果没有其他Ajax请求当前活跃将会被触发。

ajaxBeforeSend (data: xhr, options):再发送请求前,可以被取消。

ajaxSend (data: xhr, options):像 ajaxBeforeSend,但不能取消。

ajaxSuccess (data: xhr, options, data):当返回成功时。

ajaxError (data: xhr, options, error):当有错误时。

ajaxComplete (data: xhr, options):请求已经完成后,无论请求是成功或者失败。

ajaxStop (global):如果这是最后一个活跃着的Ajax请求,将会被触发。

默认情况下,Ajax事件在document对象上触发。然而,如果请求的 context 是一个DOM节点,该事件会在此节点上触发然后再DOM中冒泡。唯一的例外是 ajaxStart & ajaxStop这两个全局事件。

$(document).on('ajaxBeforeSend', function(e, xhr, options){

// This gets fired for every Ajax request performed on the page.

// The xhr object and $.ajax() options are available for editing.

// Return false to cancel this request.

})

$.ajax({

type: 'GET',

url: '/projects',

// data to be added to query string:

data: { name: 'Zepto.js' },

// type of data we are expecting in return:

dataType: 'json',

timeout: 300,

context: $('body'),

success: function(data){

// Supposing this JSON payload was received:

// {"project": {"id": 42, "html": "

..." }}

// append the HTML to context object.

this.append(data.project.html)

},

error: function(xhr, type){

alert('Ajax error!')

}

})

// post a JSON payload:

$.ajax({

type: 'POST',

url: '/projects',

// post payload:

data: JSON.stringify({ name: 'Zepto.js' }),

contentType: 'application/json'

})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值