代码之家  ›  专栏  ›  技术社区  ›  Mr. Flibble

ajax调用在计算机断开连接时显示成功。如何诱导ajax错误状态?

  •  2
  • Mr. Flibble  · 技术社区  · 14 年前

    当我断开连接时 alert('Success: '+ json); null

    有人知道为什么这种脱节被视为成功,以及如何导致失败吗?

    $.ajax({
     url : 'post.php',
     data : { id : 123 },
     type: 'POST',
     dataType : 'json',
     success : function(json) {
        alert('Success: '+ json);
     },
     error : function(XMLHttpRequest, textStatus, errorThrown) {
         alert('Error: ' + errorThrown);
     },
     complete : function(xhr, status) { 
    
     }
    });  
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   jarcoal    14 年前

    从1.3.2升级后,我注意到对XMLHttpRequest对象调用abort会触发成功回调,而不是错误回调。在浏览了代码库之后,我注意到他们已经用一个自定义方法替换了典型的abort方法。可能与此有关。

    这里有一个关于它的帖子: http://forum.jquery.com/topic/after-aborting-an-ajax-call-success-event-is-still-being-fired

        2
  •  1
  •   volkan er    14 年前
        $.ajax({
         url : 'post.php',
         data : { id : 123 },
         type: 'POST',
         dataType : 'json',
         success : function(json) {
            if(json!=null){
                 alert('Success');
            }else{
                exceptionAjax(0,"no server data");
            }
         },
         error : function(XMLHttpRequest, textStatus, errorThrown) {
             exceptionAjax(XMLHttpRequest.statusText,XMLHttpRequest.responseText);
         },
         complete : function(xhr, status) { 
    
         }
        });
    
    function exceptionAjax(responseStatus,responseText){
          alert('Error');   
    }