Ajax has been very popular in recent years, and the essence of Ajax is XMLHttpRequest, which is the use of client-side XMLHttpRequest objects. Compared with Ajax, server-side XMLHTTP uses the XMLHttpRequest object on the server side. Although it is more inconvenient to use asynchronous requests on the server side, as a component that can send HTTP requests on the server side, there is no harm in learning about it.
Here, I am talking about using server-side XMLHttpRequest in an ASP environment, and using JScript as the language for demonstration code. Therefore, you need to understand ASP and JScript.
Server-side XMLHTTP is usually used to obtain web pages or other content from remote hosts. News aggregation systems generally use server-side XMLHTTP objects to obtain the content of the feed to be aggregated, and then use XMLDOM objects to analyze the content of the feed and retrieve the titles of the news. , author, content and other information are then stored in the database, and then the news from several data sources are displayed together. Catch shrimp is such a news aggregator, but it is not written in ASP :)
In ASP, we can use the following code to create a ServerXMLHTTP object, and this object is the basis for all operations we perform on the server side.
Copy the code code as follows:
var xmlhttp = new ActiveXObject(MSXML2.ServerXMLHTTP.5.0);
First, let’s take a look at the more useful methods of the ServerXMLHTTP object:
1. abort This method is used to cancel XMLHTTP requests. If the XMLHTTP object sends a request asynchronously, if the request still does not return after a certain time, you can use this method to cancel the request.
2. getAllResponseHeaders The return value of this method is a string, which is equivalent to the header of the HTTP request without the request method, URI and protocol version information.
3. getResponseHeader This method is used to obtain the specified header information. The more useful one is to obtain the Content-Type, Referer, etc. of the returned data.
4. open initializes a request using the specified request method, URI, synchronization method, authentication information, etc.
5. send sends an HTTP request and waits for the response data to be received. Note that if the request is sent in a synchronous manner, the send method will not return immediately after being called, but will wait until the request is completed. When requesting in an asynchronous method, it will Return immediately. In addition, the send method has an optional parameter body, which represents the data to be sent, which is more useful when using the POST method.
6. setTimeout sets the four timeouts of the ServerXMLHTTP object, which are: domain name resolution, connecting to the server, sending data, and receiving responses. You can control the ServerXMLHTTP object by setting the corresponding timeout period to prevent ServerXMLHTTP from failing to return in time and causing the program to stop responding.
7. setRequestHeader sets the header of the request. In the client XMLHttpRequest, it is usually used to set the data type of the request, or identify the request method, etc. For example, jquery will add the header identifier X-Request-With to indicate that the request is from the XMLHttpRequest object. Issued to facilitate the server to take corresponding actions.
8. waitForResponse When sending a request asynchronously, you can use this method to control the request process. In server-side scripts, you cannot directly use callback functions to control asynchronous requests like the client does, and there is no corresponding function to use the program to sleep for a certain period of time. Therefore, in order to wait for the request to return, we can use this method to wait for a certain period of time. .
In addition, there are other methods, such as getOption, setOption, setProxy, etc. These methods are rarely used, so they will not be introduced here. Friends who need to know more can refer to MSDN.
Next, let's look at the properties of the ServerXMLHTTP object:
1. onreadystatechange The callback function when the XMLHTTP object state changes. This attribute lays a foundation for asynchronous operations, allowing the program to know whether the XMLHTTP operation has been completed without querying the XMLHTTP object state.
2. readyState XMLHTTP object status has 5 values, from 0 to 4, which respectively represent:
0 - Not initialized, the state of the object when it was just created using new ActiveXObject(MSXML.ServerXMLHTTP.5.0)
1 - Loading. At this time, the open method has been called, but the send method has not been used to send data.
2 - Already loaded, the send method has been called to send data, but there is no response stream available yet.
3 - Interacting and receiving data. At this time, you can use the responseBody and responseText attributes to obtain part of the data that has been obtained.
4 - Complete the request, all data has been received
Normally, we only need to determine state 4. At this time, all the data has been loaded, and the required data can be obtained using the responseBody or responseText attributes.
3. status HTTP response status code, which should be 200 under normal circumstances. If the requested resource does not exist, 404 will be returned. There are other status codes such as server error 500, etc.
4. statusText HTTP response status text, used to describe the meaning of the response status code, such as OK in 200 OK, Not Found in 404 Not Found
5. responseBody is the byte array of response data. This can be used directly in VBScript, but it needs to be converted in JScript.
6. responseText obtains response data in text form
7. responseXML returns the response data as an XMLDOM object, which is particularly useful when the requested data is an XML document.
8. responseStream response stream object, this attribute is not commonly used