a simple Ajax
<html><head><script type="text/javascript">function loadXMLDoc(){if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safarixmlhttp=new XMLHttpRequest(); //The keystone of AJAX is the XMLHttpRequest object.//All modern browsers support the XMLHttpRequest object (IE5 and IE6 uses an ActiveXObject).}else{// code for IE6, IE5xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}//The onreadystatechange event is triggered four times, one time for each change in readyState.xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200)//When using async=true, //specify a function to execute when the response is ready in the onreadystatechange event://Using async=false is not recommended, but for a few small requests this can be ok.//Remember that the JavaScript will NOT continue to execute, //until the server response is ready. If the server is busy or slow, the //application will hang or stop. { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; //If the response from the server is not XML, use the responseText property. }}xmlhttp.open("GET","ajax_info.txt",true); //to behave as AJAX, the async parameter of the //open() method has to be set to true:xmlhttp.send();}</script></head><body><div id="myDiv"><h2>Let AJAX change this text</h2></div><button type="button" >Change Content</button></body></html>Send a Request To a ServerMethod Descriptionopen(method,url,async)Specifies the type of request, the URL, and if the request should be handled asynchronously or not.
method: the type of request: GET or POST
url: the location of the file on the server
async: true (asynchronous) or false (synchronous)send(string)Sends the request off to the server.
string: Only used for POST requestsalways use POST requests when:
[*]A cached file is not an option (update a file or database on the server)
[*]Sending a large amount of data to the server (POST has no size limitations)
[*]Sending user input (which can contain unknown characters), POST is more robust and secure than GET
To POST data like an HTML form, add an HTTP header with setRequestHeader(). Specify the data you want to send in the send() method:
xmlhttp.open("POST","ajax_test.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
Method DescriptionsetRequestHeader(header,value)Adds HTTP headers to the request.
header: specifies the header name
value: specifies the header value
Server Response
To get the response from a server, use the responseText or responseXML property of the XMLHttpRequest object.
Property DescriptionresponseTextget the response data as a stringresponseXMLget the response data as XML data
The responseText Property
If the response from the server is not XML, use the responseText property.
The responseText property returns the response as a string, and you can use it accordingly:
The responseXML Property
If the response from the server is XML, and you want to parse it as an XML object, use the responseXML property:
Example
cd_catalog.xml:
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
Request the file cd_catalog.xml and parse the response:
xmlDoc=xmlhttp.responseXML;
var txt="";
x=xmlDoc.getElementsByTagName("ARTIST");
for (i=0;i<x.length;i++)
{
txt=txt + x.childNodes.nodeValue + "<br />";
}
document.getElementById("myDiv").innerHTML=txt;
The onreadystatechange event
When a request to a server is sent, we want to perform some actions based on the response.
The onreadystatechange event is triggered every time the readyState changes.
The readyState property holds the status of the XMLHttpRequest.
Three important properties of the XMLHttpRequest object:
Property DescriptiononreadystatechangeStores a function (or the name of a function) to be called automatically each time the readyState property changesreadyStateHolds the status of the XMLHttpRequest. Changes from 0 to 4:
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is readystatus200: "OK"
404: Page not foundIn the onreadystatechange event, we specify what will happen when the server response is ready to be processed.
When readyState is 4 and status is 200, the response is ready:
页:
[1]