Using xmlhttp objects on the Web Client can easily exchange data with the server. We can obtain and send any type of data, or even binary data, to the server. Xmlhttp technology is also the method used to exchange data with servers for most refreshing pages. This method is more convenient and economical than the previous method to hide iframe.
At the same time, we are glad that xmlhttp is not unique to IE. Although it is not yet W3C standard, it is supported by IE, Netscape/Mozilla, and Safari. In IE, we use new ActiveXObject ('msxml2. XMLHTTP ') or new ActiveXObject ("Microsoft. XMLHTTP. In Netscape/Mozilla and Safari, use new XMLHttpRequest () to obtain the xmlhttp object instance. For example, in IE, we usually use the following method:
Var xmlhttp = null;
Try
{
Xmlhttp = new ActiveXObject ("MSXML2.XMLHTTP ");
}
Catch (e)
{
Try
{
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (e2 ){}
}
Using an xmlhttp object is actually not difficult. It has 6 methods and 8 attributes. However, it mainly provides two execution modes: Synchronous mode and asynchronous mode. The synchronous mode can control the program process accurately. However, if the server's Response is too slow, the browser will die and lose the corresponding problem. the asynchronous mode is used to control the process in event-triggered mode, it may cause some unexpected issues to the program running, because you do not know what operations the user will perform in the browser when the client waits for the server Response.
The following is a simple example of synchronizing server data:
Function GetRemoteData (url)
{
Var xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
Try
{
XMLHTTP. Open ('get', URL, false );
If (XMLHTTP. Status = 200)
{
Return XMLHTTP. responsetext;
}
Throw '';
}
Catch (E)
{
Return '';
}
}
Lists the attributes and methods of the XMLHTTP object (from the IXMLHTTPRequest Interface ):
|
| Name |
Type |
Description |
| Onreadystatechange |
N/ |
Specifies the event processing function called when the ready status changes. It is only used for asynchronous operations. |
| ReadyState |
Long |
Asynchronous Operation Status: Not initialized (0), loading (1), loaded (2), InterAction (3), completed (4) |
| ResponseBody |
Variant |
Returns the response body as an unsigned byte array. |
| ResponseStream |
Variant |
Returns the response body as an ADO Stream object. |
| ResponseText |
String |
Returns the response body as a text string. |
| ResponseXML |
Object |
Resolve the response body to an XMLDocument object through XMLDom |
| Status |
Long |
HTTP status code returned by the server |
| StatusText |
String |
Server HTTP Response line status |
|
|
| Name |
Desciption |
| Abort |
Cancel current HTTP Request |
| GetAllResponseHeaders |
Retrieve all header fields from the response information |
| GetResponseHeader |
Obtain an HTTP header value from the response body. |
| Open (method, url, boolAsync, bstrUser, bstrPassword) |
Open a connection to the HTTP server |
| Send (varBody) |
Set the header field of a request |
| SetRequestHeader (bstrHeader, bstrValue) |
Send a request to the HTTP server. Body. |
|
It is obvious that the open method is troublesome and involves a lot of parameters. Their meanings are as follows:
|
| Parameter |
Description |
| Method |
HTTP communication methods, such as GET, HEAD, POST, PUT, DELETE, and CONNECT |
| Url |
The URL of the server that receives data. The URL can contain QueryString. |
| BoolAsync |
A boolean identifier indicating whether the request is asynchronous. In asynchronous communication mode, the client does not wait for the server to respond. In synchronous mode, the client waits until the server returns a message to perform other operations. |
| BstrUser |
User ID for Server Authentication |
| BstrPassword |
User Password for Server Authentication |
|
Asynchronous Communication Example:
Xmlhttp. open ("GET", "default. aspx", true );
Xmlhttp. onreadystatechange = function ()
{
If (xmlhttp. readyState = 4)
{
Alert (xmlhttp. responseText );
}
}
Xmlhttp. send (null );
In fact, using xmlhttp is so simple, complicated is the way Server data is organized, and developers need to be familiar with Client and Server development at the same time, in order to get twice the result with half the effort. But it seems that this stuff has nothing to do with xml for a long time. How is it called xmlhttp? We noticed that there is a responseXML in the response data type, but it parses the returned XMLDocument to be the content of XMLDOM. It has little to do with the communication between xmlhttp and the server. Let's talk about it later.
Source: http://blog.csdn.net/fu_qi_ming/archive/2005/04/13/345319.aspx