reenadak

2018-08-15_basic ajax

Sep 25th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <body>
  3. <script language="javascript" type="text/javascript">
  4.   function send_with_ajax()
  5.   {
  6.     document.getElementById("req_text").innerHTML = "Process Started...";
  7.    
  8.      if(window.XMLHttpRequest || window.ActiveXObject)
  9.      {
  10.        if(window.ActiveXObject)
  11.        {
  12.          try
  13.            {
  14.              xhr = new ActiveXObject("Msxml2.XMLHTTP");
  15.            }
  16.          catch(exception)
  17.            {
  18.              xhr = new ActiveXObject("Microsoft.XMLHTTP");
  19.            }
  20.        }
  21.        else
  22.        {
  23.           xhr = new XMLHttpRequest();
  24.        }
  25.      }
  26.      else
  27.      {
  28.         alert("Your browser does not support XMLHTTP Request...!");
  29.      }
  30.  
  31.      xhr.open("GET", "ajax_demo.txt", true);    // Make sure file is in same server
  32.      xhr.send(null);
  33.  
  34.       xhr.onreadystatechange = function()
  35.       {
  36.          if (xhr.readyState == 4)
  37.          {
  38.              if(xhr.status == 200)
  39.              {
  40.                  document.getElementById("req_text").innerHTML = "Response State : " + xhr.readyState + "<br />Response Text: <br />" + xhr.responseText;  
  41.                }
  42.             else
  43.             {
  44.                  document.getElementById("req_text").innerHTML = "Error Code: " + xhr.status + "<br />" + "Error Message: " + xhr.statusText;
  45.               }
  46.           }
  47.       };
  48.   }
  49. </script>
  50. <button onClick="send_with_ajax()">Get Contain ok</button>
  51. <p id="req_text"></p>
  52. </body>
  53. </html>
Advertisement
Add Comment
Please, Sign In to add comment