jquery中的ajax方面的方法所触发的完整事件流演示
$(function(){// 绑定ajax全局事件$(document).ajaxStart(onStart) .ajaxStop(onStop) .ajaxSend(onSend) .ajaxComplete(onComplete) .ajaxSuccess(onSuccess) .ajaxError(onError);function onStart(event) {logEvent(event);}function onStop(event) {logEvent(event);}function onSend(event, xhr, settings) {logEvent(event, xhr, settings);}function onComplete(event, xhr, settings) {logEvent(event, xhr, settings);}function onSuccess(event, xhr, settings) {logEvent(event, xhr, settings);}function onError(event, xhr, settings, err) {logEvent(event, xhr, settings, err);}// 打印事件function logEvent(event, xhr, settings, err) {var s = '事件名(event):'; s += event.type && event.type+"(全局)" || event;//状态码if (xhr && xhr.readyState > 1) s += ';状态码(statusCode): ' + xhr.status;//数据源路径if (settings) s += ';数据源路径(url): ' + settings.url;//错误信息if (err) s += ';错误(error): ' + err;// 向面板添加新的消息$('#log').append('<div>'+s+'</div>');}//成功获取获取数据$("#getSuccessData").click(function(){$("#log").html("");$.ajax({ type: "get", url: "data.xml", beforeSend : function(){ logEvent("beforeSend"); }, success : function(data){ logEvent("success"); }, complete : function(event){ logEvent("complete"); }});})$("#getErrorData").click(function(){$("#log").html("");$.ajax({ type: "get", url: "error.xml", beforeSend : function(){ logEvent("beforeSend"); }, success : function(data){logEvent("success"); }, complete : function(){ logEvent("complete"); }});}) });
页:
[1]