ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: XML :: XMLHttpRequest ¶ÔÏó

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

XML
XML DHTMLÐÐΪ
XML Ïà¹Ø¼¼Êõ
XML ±à¼­Æ÷
XML ÕªÒª
XML ʵÀý
XML×Ö·û±àÂë
xml ÎĵµÊ÷
IEºÍ»ðºü¶ÁÈ¡XML·..

XMLHttpRequest ¶ÔÏó


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-29 ÐÞ¸Ä:2007-08-17 ä¯ÀÀ:6047 :: ::

The XMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 9, and Netscape 7.
XMLHttpRequest ¶ÔÏó¿É±» Internet Explorer 5.0+¡¢Safari 1.2¡¢Mozilla 1.0 / Firefox¡¢Opera 9 ºÍ Netscape 7.Ö§³Ö


What is an HTTP Request?
ʲôÊÇHTTP ÇëÇó£¿

With an HTTP request, a web page can make a request to, and get a response from a web server - without reloading the page. The user will stay on the same page, and he or she will not notice that scripts might request pages, or send data to a server in the background.
ͨ¹ýHTTPÇëÇó£¬Ò³Ãæ¿ÉÒÔÖ±½ÓÏò·þÎñÆ÷·¢³öÇëÇ󣬴Ӷø»ñÈ¡À´×Ô·þÎñÆ÷¶ËµÄ»ØÓ¦£¬¶ø²»ÐëÒªÔÙÖØÐ¼ÓÔØÒ³Ãæ¡£Ê¹ÓÃÕßÈÔÈ»»áÍ£ÁôÔÚÏàͬµÄÒ³ÃæÉÏ£¬Ëû/Ëý²¢²»»áÒâʶµ½½Å±¾³ÌÐòÒѾ­Ë½ÏÂÀïÏò·þÎñÆ÷·¢³öÇëÇó»ò·¢ËÍÊý¾ÝÁË¡£

By using the XMLHttpRequest object, a web developer can change a page with data from the server after the page has loaded.
ͨ¹ýʹÓÃXMLHttpRequest ¶ÔÏó£¬ÍøÕ¾¿ª·¢ÕßÔÚ¼ÓÔØÒ³Ãæºó¿ÉÒÔ»ñÈ¡À´×Ô·þÎñÆ÷µÄÊý¾ÝÀ´¸üÐÂÒ³Ãæ¡£

Google Suggest is using the XMLHttpRequest object to create a very dynamic web interface: When you start typing in Google's search box, a JavaScript sends the letters off to a server and the server returns a list of suggestions.
Google Suggest ÊÇʹÓÃXMLHttpRequest ÏóÀ´´´½¨Ò»¸ö¼«¾ß¶¯Ì¬Ð§¹ûµÄÍøÒ³½çÃæ£ºµ±ÄãÔÚGoogleµÄËÑË÷¿òÄÚÊäÈëËÑË÷ÐÅϢʱ£¬Ò»·ÝJavascript ½Å±¾³ÌÐò»á½«×Öĸ·¢Ë͸ø·þÎñÆ÷£¬¶ø·þÎñÆ÷Ôò·µ»ØÒ»ÏµÁн¨Òé¡£


Is the XMLHttpRequest Object a W3C Standard?
XMLHttpRequest¶ÔÏóÊÇW3C±ê×¼Âð£¿

The XMLHttpRequest object is not a W3C standard.
XMLHttpRequest ¶ÔÏó²»ÊÇW3C±ê×¼¡£

The W3C DOM Level 3 "Load and Save" specification contains some similar functionality, but these are not implemented in any browsers yet. So, at the moment, if you need to send an HTTP request from a browser, you will have to use the XMLHttpRequest object.
“W3C DOM Level 3” µÄ“ÏÂÔØºÍ±£´æ”¹æ·¶°üº¬ÁËһЩÏàËÆµÄ¹¦ÄÜ£¬µ«ÕâЩ¹¦ÄÜûÓÐÔÚÈκÎä¯ÀÀÆ÷ÖÐÖ´ÐС£Òò´ËÏÖÔÚ£¬Èç¹ûÄãÏëÒª´Óä¯ÀÀÆ÷¶Ë·¢ËÍÒ»·ÝHTTPÒªÇó£¬Äã¾Í±ØÐëʹÓÃXMLHttpRequest¶ÔÏó¡£


Creating an XMLHttpRequest Object
´´½¨Ò»¸öXMLHttpRequest¶ÔÏó

For Mozilla, Firefox, Safari, Opera, and Netscape:
Ϊ Mozilla¡¢, Firefox¡¢Safari¡¢Opera ºÍ Netscape ´´½¨Ò»¸ö XMLHttpRequest ¶ÔÏó¡£

var xmlhttp=new XMLHttpRequest()

For Internet Explorer:
ΪIE´´½¨£º

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

Example
°¸Àý

<script type="text/javascript">
var xmlhttp
function loadXMLDoc(url)
{
xmlhttp=null
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
else
{
alert("Your browser does not support XMLHTTP.")
}
}
function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
// ...some code here...
}
else
{
alert("Problem retrieving XML data")
}
}
}
</script>

Try it yourself using JavaScript
³¢ÊÔʹÓÃJavaScript ´´½¨

The syntax is a little bit different in VBScript: Try it yourself using VBScript
ÔÚÓï·¨ÉÏVBscript»áÓе㲻ͬ£º³¢ÊÔʹÓà VBScript ´´½¨ ¡£

Note: An important property in the example above is the onreadystatechange property. This property is an event handler which is triggered each time the state of the request changes. The states run from 0 (uninitialized) to 4 (complete). By having the function xmlhttpChange() check for the state changing, we can tell when the process is complete and continue only if it has been successful.
×¢Òâ:
ÒÔÉÏÀý×ÓµÄÖØÒªÌØÐÔÊÇ“onreadystatechange[ ËæÊ±×´Ì¬¿É±ä ]” ÊôÐÔ¡£¸ÃÊôÐÔÊÇÒ»¸ö“ʼþ´¦ÀíÕß”£¬Ã¿µ±ÇëÇó״̬¸Ä±äʱ£¬Ëü¶¼»á±»´¥·¢¡£×´Ì¬´Ó0£¨³õʼ»¯£©µ½4£¨Íê³É£©¡£Í¨¹ýxmlhttpChange() ¹¦ÄܶÔ״̬¸Ä±äµÄ¼ì²é£¬ÎÒÃÇÄܹ»Çø·Ö³ÌÐòÊÇ·ñ½áÊø£¬»ò½«¼ÌÐø¡£


 

Why are we Using Async in our Examples?
ΪʲôÎÒÃÇÒªÔÚ°¸ÀýÖÐʹÓÃAsync£¿

All the examples here use the async mode (the third parameter of open() set to true).
Õâ¶ùËùÓÐÀý×Ó¶¼ÓÃÁËasync ģʽ£¨open() ÔªËØµÄµÚÈý¸ö²ÎÊý±»ÉèÖÃΪ“Õæ”£©

The async parameter specifies whether the request should be handled asynchronously or not. True means that script continues to run after the send() method, without waiting for a response from the server. false means that the script waits for a response before continuing script processing. By setting this parameter to false, you run the risk of having your script hang if there is a network or server problem, or if the request is long (the UI locks while the request is being made) a user may even see the "Not Responding" message. It is safer to send asynchronously and design your code around the onreadystatechange event!
Async ²ÎÊýÖ¸¶¨ÁË·¢³öµÄÇëÇóÊÇ·ñÐèÒª±»Í¬Ê±´¦Àí¡£“Õæ£¨TRUE£©”±íʾ£ºÔÚsend() ·½·¨±»Ö´Ðк󣬽ű¾³ÌÐò¼ÌÐøÔËÐУ¬²»µÈ´ý·þÎñÆ÷¶ËµÄ»ØÓ¦¡£“¼Ù£¨FALSE£©”±íʾ£º½Å±¾³ÌÐò¼ÌÐøÔËÐÐǰÐëÒªµÈ´ý·þÎñÆ÷¶ËµÄ»ØÓ¦¡£½«ÉèÖòÎÊýΪ¼Ù£¬Èç¹ûÍøÂç»ò·þÎñÆ÷ÓÐÎÊÌ⣬»òÕßÇëÇóʱ¼ä¹ý³¤£¨·¢³öÇëÇóʱUIËø¶¨£©£¬ÄãµÄ½Å±¾³ÌÐò¾Í»á±»¹ÒÆð£¬Ê¹ÓÃÕß¿ÉÄܻῴµ½“ûÓлØÓ¦”µÄÐÅÏ¢¡£¸ü¼Ó°²È«µÄ×ö·¨ÊÇ£ºÍ¬²½·¢ËÍÐÅÏ¢£»»òÕߣ¬ÔÚ“onreadystatechange[ ËæÊ±×´Ì¬¿É±ä ]” ʼþÏÂÉè¼Æ´úÂë¡£


 

More Examples
¸ü¶à¾ÙÀý

ͨ¹ýXML HTTP£¨JavaScript£©½«Îı¾ÎļþÔØÈëÒ»¸ödivÔªËØ

ͨ¹ýXML HTTP£¨JavaScript£©·¢³öÒ»¸öHeadÇëÇó

ͨ¹ýXML HTTP£¨JavaScript£©·¢³öÒ»¸öÌØ¶¨µÄHeadÇëÇó

ͨ¹ýXML HTTP£¨JavaScript£©ÁгöÒ»¸öXMLÎļþÖеÄÊý¾Ý


The XMLHttpRequest Object Reference
XMLHttpRequest ¶ÔÏó²ÎÊý

Methods
·½·¨

·½·¨ ÃèÊö
abort() Cancels the current request
È¡Ïûµ±Ç°ÇëÇó
getAllResponseHeaders() Returns the complete set of http headers as a string
ÒÔ×Ö·û´®ÐÎʽ·µ»ØÍêÕûµÄhttp headersÊôÐÔ
getResponseHeader("headername") Returns the value of the specified http header
·µ»ØÖ¸¶¨µÄhttp headerÖµ
open("method","URL",async,"uname","pswd") Specifies the method, URL, and other optional attributes of a request
Ö¸¶¨·½·¨¡¢URL£¨³¬Á´½Ó£©ÒÔ¼°ÆäËüÓëÇëÇóÏà¹ØµÄ¸½¼ÓÐÅÏ¢

 

The method parameter can have a value of "GET", "POST", or "PUT" (use "GET" when requesting data and use "POST" when sending data (especially if the length of the data is greater than 512 bytes.
·½·¨²ÎÊý¿ÉÒÔ»ñÈ¡“GET”¡¢“POST”ÒÔ¼°“PUT”µÄÖµ£»Èç¹û·¢Ë͵ÄÊý¾Ý³¤¶È´óÓÚ512×Ö½Ú£¬ÄÇôÇëʹÓÓPOST”ÃüÁî¡£

The URL parameter may be either a relative or complete URL.
URL²ÎÊý¿ÉÒÔÊÇÕûÌõURLµÄÐÅÏ¢»òÕßÊÇÆäÖÐÏà¹ØµÄÒ»²¿·Ý

The async parameter specifies whether the request should be handled asynchronously or not. true means that script processing carries on after the send() method, without waiting for a response. false means that the script waits for a response before continuing script processing
“Async”²ÎÊýÖ¸¶¨ÁË·¢Ë͵ÄÇëÇóÊÇ·ñÐèҪͬ²½´¦Àí¡£“True£ºÕæ”±íʾ£º½Å±¾³ÌÐòÔÚsend()·½·¨Ö´Ðкó£¬ÎÞÐëµÈ´ý·þÎñÆ÷»ØÓ¦±ã¿ÉÒÔ¼ÌÐø´¦Àí£»¶ø“False£º¼Ù”Ôò±íʾ£º½Å±¾³ÌÐòÔÚ´¦Àí֮ǰÐëÒªµÈ´ý·þÎñÆ÷¶ËµÄ»ØÓ¦

send(content) Sends the request
·¢ËÍÇëÇó
setRequestHeader("label","value") Adds a label/value pair to the http header to be sent
½«“±êÇ©£¨label£©/ Öµ£¨value£©”³É¶ÔÌí¼Óµ½½«Òª±»·¢Ë͵ēhttp header”ÖÐ

Properties
ÊôÐÔ

ÊôÐÔ ÃèÊö
onreadystatechange An event handler for an event that fires at every state change
Ò»¸öÄܹ»¼¤·¢ËùÓÐ״̬¸Ä±äµÄʼþ´¦ÀíÆ÷
readyState Returns the state of the object:
·µ»Ø¶ÔÏóµÄ¾ßÌå״̬

0 = uninitialized
0 = δ±»³õʼ»¯

1 = loading
1 = ÕýÔÚ¼ÓÔØµÄ

2 = loaded
2 = Òѱ»¼ÓÔØµÄ

3 = interactive
3 = ½»»¥Ê½µÄ

4 = complete
4 = ÒÑÍê³ÉµÄ

responseText Returns the response as a string
ÒÔ×Ö·û´®µÄÐÎʽ·µ»ØÏìÓ¦
responseXML Returns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties
ÒÔXMLµÄÐÎʽ·µ»ØÏìÓ¦¡£Õâ¸öÊôÐÔ·µ»ØÁËÒ»¸öXMLÎĵµ¶ÔÏ󣬸öÔÏó¿ÉÒÔͨ¹ýW3C DOMÍøÂç½ÚµãÊ÷µÄ·½·¨ºÍÊôÐÔÀ´¼ìÑéºÍ½âÎö¡£
status Returns the status as a number (e.g. 404 for "Not Found" or 200 for "OK")
·µ»ØÒÔÊý×ÖÐÎʽ±íʾµÄ״̬ ( È磺404±íʾ"Not Found£ºÎ´ÕÒµ½"£» 200 Ϊ "OK£ºÍ¨¹ý" )
statusText Returns the status as a string (e.g. "Not Found" or "OK")
·µ»ØÒÔ×Ö·û´®ÐÎʽ±íʾµÄ״̬ (È磺 "Not Found£ºÎ´ÕÒµ½" »ò "OK£ºÍ¨¹ý")

ÆÀÂÛ (2) 1 All