简体   繁体   中英

XMLHTTPRequest onreadystatechange readyState is always 1?

My readyState in my onreadystatechange callback seems to be stuck at 1.

  let req
  function reloadData(){
    let now = new Date()
    url = 'liveData?' + now.getTime()
    try {
      req = new XMLHttpRequest()
    } catch (e) {
      try {
        req = new ActiveXObject("Msxml2.XMLHTTP")
      } catch (e) {
        try {
          req = new ActiveXObject("Microsoft.XMLHTTP")
        } catch (oc) {
          alert("No AJAX Support")
          return
        }
      }
    }

    req.onreadystatechange = processData
    req.open("GET", url, true)
    req.send(null)
  }
  function processData(){
    alert(req.readyState)
    // If req shows "complete"
    if (req.readyState == 4){
      dataDiv = document.getElementById('currentData')
      // If "OK"
      if (req.status == 200)
      {
        // Set current data text
        dataDiv.innerHTML = req.responseText
      }
      else
      {
        // Flag error
        dataDiv.innerHTML = '<p>There was a problem retrieving data: ' + req.statusText + '</p>'
      }
    }
  }

the request itself works and I log when /liveData gets a request and I get that log as well. I have no errors and the alert itself works so it is calling properly, but the readyState is at 1 at all times.

原来服务器只是没有发送响应,谢谢异端猴子在评论中指出这一点。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM