2014-09-19 161 views
0

有沒有辦法在IE9中的ajax調用中檢查與服務器的連接狀態?IE9 - 檢查服務器連接狀態

林在支持XHR的瀏覽器使用此:

AJS.$.ajax({ 
    url : "path_to_server", 
    type : 'DELETE', 
    async:false, 
    data:JSON.stringify(resquestData) 
}).done(function(){ 
    doSomething(); 
}).fail(function(response,textStatus, xhr){ 
    if(xhr.code == 19){ // where i check if it is a NetworkError 
     alert("connection lost"); 
    }else{ 
     alert("error"); 
    } 
} 
); 

我的問題是,我不能讓這個解決方案在IE9(或更少)的作品,因爲瀏覽器不支持XHR(XmlHttpRequest的)。任何方案?

問候

[編輯]

我嘗試了AJAX功能之前創建的ActiveXObject:

AJS.$("#import").click(function() { 
    var xmlhttpie; 
    if (window.ActiveXObject) { 
     try { 
      xmlhttpie = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try { 
       xmlhttpie = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e) { 
       xmlhttpie = false; 
      } 
     } 
    } 
    AJS.$.ajax({ 
     url : "path_to_server", 
     type : 'DELETE', 
     async:false, 
     data:JSON.stringify(resquestData) 
    }).done(function(){ 
      doSomething(); 
    }).fail(function(response,textStatus, xhr){ 
      if(xhr.code == 19){ // where i check if it is a NetworkError 
      alert("connection lost"); 
      }else{ 
       alert("error"); 
      } 
    } 
); 
} 

但在IE9調試(內部的Ajax功能),當我得到這個(其中「 A「是xmlhttpie):

鏈接 - >http://prntscr.com/4o9grh

回答

1

只需在您的ajax調用中提供一個url並檢查它返回的內容。

$.ajax('url', { 
    statusCode: { 
    404: function() { 
     alert('Not connected to server'); 
    }, 
    200: function() { 
     alert('Connected'); 
    } 
    } 
}); 

希望這會有所幫助。

+0

謝謝喬治我會盡力 – 2014-09-19 10:20:58

+0

可能工作,如果你能夠稱之爲阿賈克斯。 – 2014-09-19 10:22:28

+0

不幸的是,這不起作用 – 2014-09-19 11:00:54