<script type="text/javascript">
function showUser(str)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
var postString = "sn_id="+str+'&content='+str+'ddd';
xmlhttp.open("POST","./stickynote_edit.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(postString);
}
</script>
Send a Request To a Server
To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object:
xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); |
Method |
Description |
open(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 requests |
Server Response
To get the response from a server, use the responseText or responseXML property of the XMLHttpRequest object.
Property |
Description |
responseText |
get the response data as a string |
responseXML |
get the response data as XML data |
The on ready state change 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 |
Description |
onreadystatechange |
Stores a function (or the name of a function) to be called automatically each time the readyState property changes |
readyState |
Holds 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 ready |
status |
200: "OK" 404: Page not found |
留言列表