topical media & game development

talk show tell print

professional-javascript-16-IEXMLHttpRequestExample4.htm / htm



  <html>
      <head>
          <title>XML HTTP Request Example</title>
          <script type="text/javascript">
          
                  function createXMLHTTP() {
                  
                      var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
                                           "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
                                           "Microsoft.XMLHTTP"];
                                           
                      for (var i=0; i < arrSignatures.length; i++) {
                          try {
                          
                              var oRequest = new ActiveXObject(arrSignatures[i]);
                              
                              return oRequest;
                          
                          } catch (oError) {
                              //ignore
                          }
                      }          
                  
                      throw new Error("MSXML is not installed on your system.");               
                  }
                  
                  function getServerInfo() {
                          var oRequest = createXMLHTTP();
                          oRequest.open("get", "example.txt", true);
                          oRequest.onreadystatechange = function () {
                              if (oRequest.readyState == 3) {
                                  oRequest.abort();
                              } else if (oRequest.readyState == 4) {
                                  alert("Status is " + oRequest.status + " (" + oRequest.statusText + ")");
                                  alert("Response text is: " + oRequest.responseText);
                              }
                          }
                          oRequest.send(null);
  
                  }
  
          </script>
      </head>
      <body>
          <p>Click the button to make a call to the server using an asynchronous XML HTTP request.
          The call is cancelled before the response is returned, so you'll see nothing.</p>
          <input type="button" onclick="getServerInfo()" value="Get Server info" />
      </body>
  </html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.