AJAX加载器GIF而异步POST请求在IE中不起作用

时间:2009-10-23 11:25:14

标签: javascript html ajax

我正在尝试在执行异步POST请求时显示AJAX加载程序gif。不幸的是,这不适用于Interet Explorer!显示了Gif,但请求过程似乎分别停止了更改的webcontent将不会显示。在FF,Opera,Safari一切都很好!有什么想法吗?

http_request.onreadystatechange = function() 
 {
   if (http_request.readyState < 4)
   {
  var waitingPageBody = '< img src="/img/ajaxloader.gif" alt="request in progress..."/>';
  document.body.innerHTML = waitingPageBody;
   }
   else //if (http_request.readyState == 4)
   {
  if (http_request.status == 200)
  {
    document.body.innerHTML = http_request.responseText;

  }//end of if (http_request.status == 200)
  else
  {//other http statuses
    alert("There was a problem");
  }
   } //end of else if http_request.readyState == 4
 }

...

2 个答案:

答案 0 :(得分:2)

窗口将冻结,因为请求是同步的。每当执行javascript代码时浏览器都会冻结,并且运行同步请求相当于在浏览器等待响应的整个过程中运行javascript代码。

答案 1 :(得分:1)

您已向我们展示了事件处理代码,但您是在呼叫open()send()吗?

您的事件处理代码似乎适用于FF3.5,IE6和IE8:
http://jsbin.com/ocoka(可编辑通过:http://jsbin.com/ocoka/edit

完整来源:

<!DOCTYPE html>
<html>
<head>
  <title>Sandbox</title>
  <script>
    function load() {
      var http_request = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
      http_request.open('GET', 'http://jsbin.com/blank.html', true);

      http_request.onreadystatechange = function() {
        if (http_request.readyState < 4) {
          var waitingPageBody = 'request in progress...';
          document.body.innerHTML = waitingPageBody;
        }
        else {
          //if (http_request.readyState == 4)
          if (http_request.status == 200) {
            document.body.innerHTML = '<pre>' + entity(http_request.responseText) + '</pre>';
          }//end of if (http_request.status == 200)
          else {
            //other http statuses
            alert("There was a problem");
          }
        } //end of else if http_request.readyState == 4
      };

      http_request.send();
    }

    function entity(str) {
      return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    };
  </script>
</head>
<body onload="load();">
</body>
</html>

编辑:

根据您的评论,您正在发出同步请求。在这种情况下,您根本不需要(不应该?)使用事件处理程序,只需在调用send()之前和之后运行代码:

托管演示(在FF3.5,IE6和IE8中测试):
http://jsbin.com/elehu(可编辑通过:http://jsbin.com/elehu/edit

完整来源:

<!DOCTYPE html>
<html>
<head>
  <title>Sandbox</title>
  <script>
    function load() {
      var http_request = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
      http_request.open('GET', 'http://jsbin.com/blank.html', false);

      var waitingPageBody = 'request in progress...';
      document.body.innerHTML = waitingPageBody;

      /*** 
        setTimeout with 0ms delay makes sure that the
        'request in progress...' message is displayed
        before the browser thread is blocked by the send() method.
      ***/
      setTimeout(function(){
        http_request.send();
        if (http_request.status == 200) {
          document.body.innerHTML = '<pre>' + entity(http_request.responseText) + '</pre>';
        }//end of if (http_request.status == 200)
        else {
          //other http statuses
          alert("There was a problem");
        }
      }, 0);
    }

    function entity(str) {
      return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    }
  </script>
</head>
<body onload="load();">
</body>
</html>