function fetchFile()

in scomp.js [339:385]


function fetchFile(aURL, aFormat, aCallback, aAccept401) {
  var XHR = new XMLHttpRequest();
  XHR.onreadystatechange = function () {
    if (XHR.readyState == 4) {
      /*
      gLog.appendChild(document.createElement("li"))
          .appendChild(document.createTextNode(aURL + " - " + XHR.status +
                                               " " + XHR.statusText));*/
    }
    if (
      XHR.readyState == 4 &&
      (XHR.status == 200 || (XHR.status == 401 && aAccept401))
    ) {
      // so far so good
      if (
        XHR.responseXML != null &&
        aFormat == "xml" &&
        XHR.responseXML.getElementById("test").firstChild.data
      )
        aCallback(aXHR.responseXML.getElementById("test").firstChild.data);
      else if (XHR.responseText != null && aFormat == "json")
        aCallback(JSON.parse(XHR.responseText));
      else aCallback(XHR.responseText);
    } else if (XHR.readyState == 4 && XHR.status != 200) {
      // fetched the wrong page or network error...
      console.log("ERROR: XHR status " + XHR.status + " - " + aURL);
      aCallback(null);
    }
  };
  XHR.open("GET", aURL);
  if (gSocorroAPIToken) {
    // XXX: Should work but doesn't yet! We'll need to figure this out.
    //      Use this path when we have a token so bug 1143424 can be tested.
    XHR.setRequestHeader("Auth-Token", gSocorroAPIToken);
  }
  if (aFormat == "json") {
    XHR.setRequestHeader("Accept", "application/json");
  } else if (aFormat == "xml") {
    XHR.setRequestHeader("Accept", "application/xml");
  }
  try {
    XHR.send();
  } catch (e) {
    console.log("ERROR: XHR send - " + e + " - " + aURL);
    aCallback(null);
  }
}