⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dom_httprequest.asp@output=print

📁 W3Schools tutorial..web designing
💻 ASP@OUTPUT=PRINT
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>XML DOM - HttpRequest object</title>
 
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />

<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

</head>
<body>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>


<h1> The XMLHttpRequest Object</h1>

<a href="dom_nodes_clone.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodetype.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>

<hr />

<p class="intro">The XMLHttpRequest object provides a way to communicate with a 
server after a web page has loaded.</p>
<hr />

<h2>What is the XMLHttpRequest Object?</h2>
<p>The XMLHttpRequest object is <b>the developers dream</b>, because you can:</p>
<ul>
	<li>Update a web page with new data without reloading the page</li>
	<li>Request data from a server after the page has loaded </li>
	<li>Receive data from a server after the page has loaded</li>
	<li>Send data to a server in the background</li>
</ul>
<p>The XMLHttpRequest object is supported in all modern browsers.</p>
<p>Example: 
<a target="_blank" href="tryit.asp@filename=try_dom_httpsuggest">XML 
HTTP communication with a server while typing input</a></p>
<hr />

<h2>Creating an XMLHttpRequest Object</h2>
<p>Creating an XMLHttpRequest object is done with one single line of JavaScript.</p>
<p>In all modern browsers (including IE7):</p>

<table border="1" width="100%" class="ex" cellspacing="0" id="table13">
  <tr>
    <td>
      <pre>xmlhttp=new XMLHttpRequest()</pre>
    </td>
  </tr>
</table>

<p>In Internet Explorer 5 and 6:</p>

<table border="1" width="100%" class="ex" cellspacing="0" id="table14">
  <tr>
    <td>
      <pre>xmlhttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;)</pre>
    </td>
  </tr>
</table>

<h3>Example</h3>

<table border="1" width="100%" class="ex" cellspacing="0" id="table15">
  <tr>
    <td>
      <pre>&lt;script type=&quot;text/javascript&quot;&gt;
var xmlhttp;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for all new browsers
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE5 and IE6
  xmlhttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=state_Change;
  xmlhttp.open(&quot;GET&quot;,url,true);
  xmlhttp.send(null);
  }
else
  {
  alert(&quot;Your browser does not support XMLHTTP.&quot;);
  }
}</pre>
		<pre>function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = &quot;loaded&quot;
  if (xmlhttp.status==200)
    {// 200 = OK
    // ...our code here...
    }
  else
    {
    alert(&quot;Problem retrieving XML data&quot;);
    }
  }
}
&lt;/script&gt;</pre>
    </td>
  </tr>
</table>

<p><a target="_blank" href="tryit.asp@filename=try_dom_httprequest_js">Try it yourself using JavaScript</a></p>
<p><b>Note:</b> onreadystatechange is an event handler. The value (state_Change) 
is the name of a function which is 
triggered when the state of the XMLHttpRequest object changes. States run from 0 (uninitialized) 
to 4 (complete). Only when the state = 4, we can execute our code.</p>
<hr />

<h2>Why Use Async=true?</h2>
<p>Our examples use &quot;true&quot; in the third parameter of open().</p>
<p>This parameter specifies 
	whether the request should be handled asynchronously.</p>
<p>True means 
	that the script 
	continues to run after the send() method, without waiting for a response 
from the server.</p>
<p>The onreadystatechange event complicates the code. But it is the safest way 
if you want to prevent the code from stopping if you don't get a response from 
the server.</p>
<p>By setting the parameter to &quot;false&quot;, your can avoid the extra 
onreadystatechange code. Use this if it's not important to execute the rest of 
the code if the request fails.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_httprequest_js0">Try it yourself using JavaScript</a></p>
<hr />

<h2>More Examples</h2>

<p><a target="_blank" href="tryit.asp@filename=try_dom_httprequest_js1">Load a 
textfile into a div element with XML HTTP</a></p>

<p><a target="_blank" href="tryit.asp@filename=try_dom_httprequest_js2">Make a 
HEAD request with XML HTTP</a></p>

<p><a target="_blank" href="tryit.asp@filename=try_dom_httprequest_js3">Make a 
specified HEAD request with XML HTTP</a></p>

<p><a target="_blank" href="tryit.asp@filename=try_dom_httprequest_js4">List data from an XML file with XML HTTP</a></p>

<hr />

<h2>XML / ASP</h2>
<p>You can also open and send an XML document to an ASP page on 
the server, analyze the request, and send back the result.</p>
<table class="ex" id="table20" cellspacing="0" width="100%" border="1">
<tr>
<td>
<pre>&lt;html&gt;
&lt;body&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
xmlHttp=null;
if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Opera, etc.
  xmlHttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlHttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
  }
if (xmlHttp!=null)
  {
  xmlHttp.open(&quot;GET&quot;, &quot;note.xml&quot;, false);
  xmlHttp.send(null);
  xmlDoc=xmlHttp.responseText;

  xmlHttp.open(&quot;POST&quot;, &quot;demo_dom_http.asp&quot;, false);
  xmlHttp.send(xmlDoc);
  document.write(xmlHttp.responseText);
  }
else
  {
  alert(&quot;Your browser does not support XMLHTTP.&quot;);
  }
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</td>
</tr>
</table>

<p>The ASP page, written in VBScript:</p>
<table class="ex" id="table21" cellspacing="0" width="100%" border="1">
<tr>
<td>
<pre>&lt;%
set xmldoc = Server.CreateObject(&quot;Microsoft.XMLDOM&quot;)
xmldoc.async=false
xmldoc.load(request)

for each x in xmldoc.documentElement.childNodes
   if x.NodeName = &quot;to&quot; then name=x.text
next
response.write(name)
%&gt;</pre>
</td>
</tr>
</table>

<p>You send the result back to the client using the response.write property.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_httpsend2">Try it yourself</a></p>
<hr />

<h2>Is the XMLHttpRequest Object a W3C Standard?</h2>
<p>The XMLHttpRequest object is not specified in any W3C 
recommendation.</p>
<p>However, the W3C DOM Level 3 &quot;Load and Save&quot; specification contains some 
similar functionality, but these are not implemented in any browsers yet.<br />
</p>
<hr />
<a href="dom_nodes_clone.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodetype.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -