XMLHTTPRequest onreadystatechange readyState is always 1?

243 views Asked by At

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.

1

There are 1 answers

0
Steven Webster On

Turns out the server just wasn't sending a response, thank you Heretic Monkey for pointing that out in the comments.