AJAX readystate

时间:2009-11-17 19:34:37

标签: javascript ajax xmlhttprequest

此脚本每隔500毫秒向Apache Web服务器发送一个请求,以使用AJAX检索XML文档。此脚本直到最近才在下面列出的所有浏览器上正常运行。现在,XMLHttpObject readyState将等于4分钟左右但最终将在0和1之间切换。当我刷新网页时,readyState返回到4但是在一分钟左右之后再次在0和1之间切换。

我很确定在我们被迫启用IPV6之后,这个错误就开始发生了。我使用Internet Explorer 7,Internet Explorer 8,Firefox 3.0.15和Firefox 3.5.5在几台计算机上测试了该脚本。目前它只能在Internet Explorer 7和8下正常运行。

有没有人对如何在Firefox下正常运行提出任何建议?

HTML& Javascript代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<link rel="stylesheet" type="text/css" href="css/master_default.css">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Monitor</title>
</head>

<div id='screen_container'>
  <div id='data_tables'>
    <div id='data_tables_heading' class='page_container_heading'>Real-Time Data Tables</div>
    <div id="table_holder"></div>
  </div>
</div>

<script type='text/javascript' src='scripts/jquery.js'></script>

<!-- Handles AJAX server interface to retrieve XML positioning data. -->
<script language="javascript" type="text/javascript">
var ifspe_server_address = 'http://10.0.0.1:8080/';

var ifspe_positioning_dto_filename  = 'digital_receiver_ifspe_dto.xml';
var xml_http_object_positioning_dto = getXMLHttpObject();

get_ifspe_positioning_dto();

var ifspe_status_message_dto_filename  = 'ifspe_status_message_dto.xml';
var xml_http_object_status_message_dto = getXMLHttpObject();

get_ifspe_status_message_dto();

var refresh_data_id         = window.setInterval("get_ifspe_positioning_dto()", 500);
var refresh_status_messages = window.setInterval("get_ifspe_status_message_dto()", 500);
var updating_state          = 1;

// Write status message to text box.
function update_status_message(status_message_text)
{
  if(updating_state == 1)
  {
    var msg = document.getElementById("status_message_value");
    msg.innerHTML = status_message_text;
  }  
}

// Write status message to text box.
function update_debug_message(debug_message_text)
{
  if(updating_state == 1)
  {
    var msg = document.getElementById("debug_message_value");
    msg.innerHTML = debug_message_text;
  }  
}

// Retrieve XML HTTP object based on browser type.
function getXMLHttpObject()
{
  var httpObj;

  // Use this for Internet Explorer browsers.
  if(window.ActiveXObject)
  {
    httpObj = new ActiveXObject("Microsoft.XMLHTTP");
  }
  // Use this for Firefox browsers.
  else if(window.XMLHttpRequest)
  {
    httpObj = new XMLHttpRequest();
  }

  return httpObj;
}

// Sends request for XML Status Message Data Transfer Object (DTO)  file to server.
function get_ifspe_status_message_dto()
{
  var msg;
  msg = "AJAX readyState: " + xml_http_object_status_message_dto.readyState;
  update_debug_message(msg);

  // Not sure if this abortion is helpful.
  if(xml_http_object_status_message_dto.readyState > 0 && 
     xml_http_object_status_message_dto.readyState < 4)
  {
    xml_http_object_status_message_dto.abort();
  }
  else
  {
    // Added '?vers="+new Date().getTime()' to the server address so that it always gets a new XML file and not a cached version.
    xml_http_object_status_message_dto.open("GET", 
    ifspe_server_address+ifspe_status_message_dto_filename+"?vers="+new Date().getTime(), true);

    xml_http_object_status_message_dto.onreadystatechange = ifspe_status_message_dto_handler;

    xml_http_object_status_message_dto.send(" ");
  }
}

// Handles new XML file available event.
function ifspe_status_message_dto_handler()
{
  // If start/stop button is set to start (run).
  if(updating_state == 1)
  {
    if(xml_http_object_status_message_dto.readyState == 4)
    {
      if(xml_http_object_status_message_dto.status == 200)
      {  
        // Get XML data.
        var response = xml_http_object_status_message_dto.responseXML;

        var status_message_root = response.getElementsByTagName("STATUS_MESSAGE_FILE");

        // Parse xml file for status message tag.
        if(status_message_root != null)
        {
          if(status_message_root[0] != null)
          {
            var message = status_message_root[0].getElementsByTagName("MESSAGE");

            // Parse xml file for message tag.
            if(message[0] != null)
            {
              // Get message tag.
              var message_text = message[0].childNodes[0].nodeValue;

              // Update status message.
              update_status_message(message_text);
            }
          }
        }
      }
    }
  }
}

// Sends request for XML Positioning Data Transfer Object (DTO) file to server.
function get_ifspe_positioning_dto()
{
  // Not sure if this abortion is helpful.
  if(xml_http_object_positioning_dto.readyState > 0 && xml_http_object_positioning_dto.readyState < 4)
  {
    xml_http_object_positioning_dto.abort();
  }
  else
  {
    // Added '?vers="+new Date().getTime()' to the server address so that it always gets a new XML file and not a cached version.
    xml_http_object_positioning_dto.open("GET", 
    ifspe_server_address+ifspe_positioning_dto_filename+"?vers="+new Date().getTime(), true);

    xml_http_object_positioning_dto.onreadystatechange = ifspe_positioning_dto_handler;

    xml_http_object_positioning_dto.send(" ");
  }
}

// Parse XML data.
//function handle_response()
function ifspe_positioning_dto_handler()
{
  // If start/stop button is set to start (run).
  if(updating_state == 1)
  {
    if(xml_http_object_positioning_dto.readyState == 4)
    {
      if(xml_http_object_positioning_dto.status == 200)
      {
        // Get XML data.
        var response = xml_http_object_positioning_dto.responseXML;

        //
        // Log new XML data (Only works for Internet Explorer browsers).
        //
        if(logging_state == 1)
        {
          if(window.ActiveXObject)
          {          
            var digital_receiver = response.getElementsByTagName("DIGITAL_RECEIVER");
            // Process data.
          }
        } 
      }
      else if(xml_http_object_positioning_dto.status == 404)
      {
        alert("URL does not exist!");
      }
      else
      {
        //alert("Status is "+httpObj.status);
      }
    }
  }
}
</script>

</html>

1 个答案:

答案 0 :(得分:1)

我在Firefox中禁用了IPV6,这解决了这个问题。

执行此操作的说明:

  1. 在位置栏中,输入about:config并按EnterReturn。关于:配置“这可能会使您的保修失效!”可能会出现警告页面。点击我会小心,我保证!,继续到about:config页面。

  2. 在显示的列表中,在“过滤器”文本框中键入network.dns.disableIPv6。

  3. 找到network.dns.disableIPv6的条目。

  4. 如果值为false,请双击它,将值更改为true。如果该值已经为真,则IPv6已被禁用,因此不会导致您的问题。

  5. 关闭Firefox,重新启动它,然后尝试访问网站。

  6. Disable IPV6 Reference