Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery ajax事件调用顺序_Jquery_Ajax - Fatal编程技术网

jQuery ajax事件调用顺序

jQuery ajax事件调用顺序,jquery,ajax,Jquery,Ajax,假设我有一个这样的简单函数 $('body').ajaxSuccess( function(){alert('global');} ); $.post('http://www.google.com', { name: "John", time: "2pm" } , function(data,s,xhr) { alert('local'); } ); 是否可以在本地成功回调之前调用全局ajaxSuccess()函数?因为我想在本地函数进一步

假设我有一个这样的简单函数

$('body').ajaxSuccess(
    function(){alert('global');}
);

$.post('http://www.google.com', 
    { name: "John", time: "2pm" } ,
    function(data,s,xhr) {
        alert('local');
    }
);


是否可以在本地成功回调之前调用全局ajaxSuccess()函数?因为我想在本地函数进一步处理之前对结果进行全局检查。

使用默认的ajax post,而不是使用自定义post处理程序:


没有将它放在JSFIDLE中,因为发布到google.com的操作会不断失败,但这应该可以工作。

这是ajax事件调用顺序

if ( fireGlobals ){
    jQuery.event.trigger("ajaxStart");
} 
//Before send event
if ( s.beforeSend && ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
    // Abort if not done already and return
    return jqXHR.abort();
}
if ( isSuccess ) {
    // Success Event
    deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );
} else {
    // Error Event
    deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );
}

if ( fireGlobals ) {
    globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError",
        [ jqXHR, s, isSuccess ? success : error ] );
}
//Complete event
completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] );

if ( fireGlobals ) {
    globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
    // Handle the global AJAX counter
    if ( !( --jQuery.active ) ) {
        jQuery.event.trigger("ajaxStop");
    }
}

请注意,我使用的是成功但不是完整事件。如果我将“全局”改为“ajaxComplete”,那么“本地”仍将首先运行。呃,我以为你想要ajaxSuccess?为什么要将全局更改为ajaxComplete?注意,您仍然可以通过检查:if(xhr.status==200)来检查是否成功,因此您实际上不需要您的成功函数我希望以从全局到本地的顺序调用相同的事件。正如我所说,您仍然可以使用相同的功能(尽管需要另一次成功检查)。如果事件是相同的(本地成功>全局成功>本地完成>全局完成weet!我假设这是来自jQuery源的?确实是。正好来自jQuery源。
if ( fireGlobals ){
    jQuery.event.trigger("ajaxStart");
} 
//Before send event
if ( s.beforeSend && ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
    // Abort if not done already and return
    return jqXHR.abort();
}
if ( isSuccess ) {
    // Success Event
    deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );
} else {
    // Error Event
    deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );
}

if ( fireGlobals ) {
    globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError",
        [ jqXHR, s, isSuccess ? success : error ] );
}
//Complete event
completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] );

if ( fireGlobals ) {
    globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
    // Handle the global AJAX counter
    if ( !( --jQuery.active ) ) {
        jQuery.event.trigger("ajaxStop");
    }
}