XmlHttpExecutor.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // XmlHttpExecutor.js
  2. var resultElementId;
  3. function pageLoad()
  4. {
  5. resultElementId = $get("ResultId");
  6. }
  7. // This function aborts a Web request.
  8. function AbortWebRequest()
  9. {
  10. // Create the WebRequest object.
  11. wRequest = new Sys.Net.WebRequest();
  12. // Set the request Url.
  13. wRequest.set_url("getTarget.htm");
  14. // Clear the results area.
  15. resultElementId.innerHTML = "";
  16. // Set the Completed event handler,
  17. // for processing return data
  18. wRequest.add_completed(OnCompleted);
  19. // Make the request.
  20. wRequest.invoke();
  21. // Get the current executor.
  22. var executor = wRequest.get_executor();
  23. // Abort the request.
  24. executor.abort();
  25. // Check if the executor is aborted.
  26. var execAborted =
  27. executor.get_aborted();
  28. //alert("Executor aborted: " + execAborted);
  29. $get("alert2").value = "Executor aborted: " + execAborted;
  30. }
  31. // This function executes a Web request.
  32. function ExecuteWebRequest()
  33. {
  34. // Create the WebRequest object.
  35. wRequest = new Sys.Net.WebRequest();
  36. // Set the request Url.
  37. wRequest.set_url("getTarget.htm");
  38. // Set the Completed event handler
  39. // for processing return data
  40. wRequest.add_completed(OnCompleted);
  41. // Clear the results area.
  42. resultElementId.innerHTML = "";
  43. // To use executeRequest you must instantiate the
  44. // executor, assign it to the Web request instance,
  45. // then call the executeRequest function.
  46. // Note: Normally to make a Web request you use
  47. // the invoke method of the WebRequest instance.
  48. var executor = new Sys.Net.XMLHttpExecutor();
  49. wRequest.set_executor(executor);
  50. executor.executeRequest();
  51. var started = executor.get_started();
  52. //alert("Executor started: " + started);
  53. $get("alert2").value = "Executor started: " + started;
  54. }
  55. // This is the event handler called after
  56. // the Web request returns.
  57. function OnCompleted(executor, eventArgs)
  58. {
  59. if(executor.get_responseAvailable())
  60. {
  61. // Get the Web request instance.
  62. var webReq = executor.get_webRequest();
  63. // Display request Url.
  64. //alert(webReq.get_url());
  65. $get("alert1").value = webReq.get_url();
  66. // Clear the previous results.
  67. resultElementId.innerHTML = "";
  68. // Display the Web request status.
  69. resultElementId.innerHTML +=
  70. "Request Status: [" + executor.get_statusCode() + " " +
  71. executor.get_statusText() + "]" + "<br/>";
  72. // Display the Web request headers.
  73. resultElementId.innerHTML += "Headers: <br/>";
  74. // Get all the headers.
  75. resultElementId.innerHTML +=
  76. "All Request Headers: " +
  77. executor.getAllResponseHeaders() + "<br/>";
  78. // Get a specific header.
  79. resultElementId.innerHTML +=
  80. "Content-Type Header: " +
  81. executor.getResponseHeader("Content-Type") +
  82. "<br/>";
  83. // Display Web request body.
  84. resultElementId.innerHTML += "Body: <br/>";
  85. if (document.all)
  86. resultElementId.innerText +=
  87. executor.get_responseData();
  88. else
  89. // Firefox
  90. resultElementId.textContent +=
  91. executor.get_responseData();
  92. }
  93. else
  94. {
  95. if (executor.get_timedOut())
  96. alert("Timed Out");
  97. else
  98. if (executor.get_aborted()) {
  99. //alert("Aborted");
  100. $get("alert1").value = "Aborted";
  101. }
  102. }
  103. }
  104. // This is the event handler called after
  105. // the Web request returns. It is designed
  106. // for Web requests that return XML.
  107. function OnSucceededXml(executor, eventArgs)
  108. {
  109. if (executor.get_responseAvailable())
  110. {
  111. if (document.all)
  112. resultElementId.innerText += executor.get_xml().xml;
  113. else
  114. // Firefox
  115. resultElementId.textContent += "First node: " +
  116. executor.get_xml().documentElement.nodeName;
  117. }
  118. else
  119. {
  120. if (executor.get_timedOut())
  121. alert("Timed Out");
  122. else
  123. if (executor.get_aborted())
  124. alert("Aborted");
  125. }
  126. }
  127. // This function executes a Web request
  128. // to get XML data.
  129. function GetXml()
  130. {
  131. // Create the WebRequest object.
  132. wRequest = new Sys.Net.WebRequest();
  133. // Set the request Url.
  134. wRequest.set_url("getTarget.xml");
  135. // Set the Completed event handler
  136. // for processing return data.
  137. wRequest.add_completed(OnSucceededXml);
  138. // Clear the results area.
  139. if (document.all)
  140. resultElementId.innerText = "";
  141. else
  142. // Firefox
  143. resultElementId.textContent = "";
  144. // Invoke the Web request.
  145. wRequest.invoke();
  146. }