【问题标题】:IE9 - Check Server Connection StatusIE9 - 检查服务器连接状态
【发布时间】:2014-09-19 10:14:10
【问题描述】:

有没有办法在 IE9 的 ajax 调用中检查与服务器的连接状态?

我在支持 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

【问题讨论】:

    标签: ajax connection xmlhttprequest internet-explorer-9


    【解决方案1】:

    只需在您的 ajax 调用中提供一个 url 并检查它返回的内容。

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

    希望这会有所帮助。

    【讨论】:

    • 谢谢乔治,我会试试的
    • 你能在你的应用程序中执行任何 ajax
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多