例如,我有捕获json的通用代码,上面写着-“它的错误请求”。这段代码是在ajaxComplete中编写的,如何从ajaxComplete点停止执行特定的代码?
$.post('url', params,
function(json){
if (json.success == false){
alert('error')
}
if (json.success == true){
alert('success')
}
}, 'json'
);
我不想在每个ajax请求中使用此代码,而是使用如下内容:
$().ajaxComplete(function(e, xhr){
if (json.success == false){
stop_execution_of_post()
}
if (json.success == true){
proceed_execution_of_post();
}
}
而在post中,您只需编写以下代码:
$.post('url', params,
function(json){
alert('success')
}, 'json'
);
是否可以停止执行特定的ajax函数?
发布于 2010-08-26 23:18:20
您可以覆盖jQuery的内部方法,而不是使用ajaxComplete
。显然,您需要小心,因为内部方法可能会发生变化。这是针对jQuery 1.4编写的。
该方法是jQuery的httpSuccess(xhr)
。这需要一个XMLHTTPRequest
对象。当它返回AJAX时,将调用(handleError
) false
的错误处理程序。最值得注意的是,没有调用success
处理程序。请注意,始终调用complete
处理程序。
举个例子:
// Remember the old function
var oldhttpSuccess = jQuery.httpSuccess
// Create the override
jQuery.httpSuccess = function (xhr) {
// Record the result from the old function
var success = oldhttpSuccess(xhr)
// If that's bad return
if (!success) {
return success
}
// Do our custom test
if (weNeedToStop) {
return false
}
// Return the "inherited" success value
return success
}
因此,每当我们的weNeedToStop
测试为真时,对于任何ajax响应,都不会调用成功处理程序。
https://stackoverflow.com/questions/1772612
复制相似问题