In contrast to Ajax, service-side XMLHTTP is the use of XMLHttpRequest objects on the server side. Although it is not convenient to use asynchronous requests on the server side, it is no harm to learn as a component that can send HTTP requests to the server.
Here, I'm talking about using the server-side XMLHttpRequest in an ASP environment, with JScript as the language of the demo code, so you need to know about ASP and JScript.
Service-side XMLHTTP, usually used to get the remote host Web page or other content, news aggregation system is generally using the server XMLHTTP object to obtain the content of the feed to aggregate, and then use XMLDOM object to analyze the content of the feed, take out the news title, author, content and other information , then there is a database, and then the news for several data sources is displayed together. Catch shrimp is such a news aggregator, but it is not written in ASP is the:)
In ASP, we can use the following code to create a ServerXMLHTTP object, which is the basis of everything we do on the server.
Copy Code code as follows:
var xmlhttp = new ActiveXObject ("MSXML2. serverxmlhttp.5.0 ");
First, let's take a look at some of the more useful methods of ServerXMLHTTP objects:
1. Abort This method is used to cancel the XMLHTTP request. If the XMLHTTP object sends the request asynchronously, this method can be used to cancel the request if a certain time request is still not returned.
2. getAllResponseHeaders The return value of this method is a string that corresponds to the head of the HTTP request that removes the request method, URI, and protocol version information.
3. getResponseHeader This method is used to obtain the specified header information, more useful is can be used to obtain the return data Content-type, Referer and so on.
4. Open Initializes a request using the specified request method, Uri and synchronization method, and authentication information.
5. Send sends an HTTP request, waiting to receive the response data, noting that if the request is sent synchronously, the Send method call does not return immediately, but waits until the request completes before returning, but when the asynchronous method requests it, it returns immediately. In addition, the Send method has an optional parameter body that represents the data to be sent, which is useful when using the POST method.
6. SetTimeout sets the 4 timeout time for ServerXMLHTTP objects: Domain name resolution, connecting to the server, sending data, receiving responses. You can control the ServerXMLHTTP object by setting the appropriate timeout time to prevent the ServerXMLHTTP from returning in time and causing the program to stop responding.
7. setRequestHeader sets the header for the request, which in the client XMLHttpRequest is typically used to set the requested data type, or to identify the requested method, and so on, for example, jquery will increase the head ID X-request-with, Indicates that the request is emitted from the XMLHttpRequest object to facilitate the server to make the appropriate action.
8. waitForResponse can use this method to control the requested process when sending a request asynchronously. In a server-side script, it is not possible to use the callback function directly to control the asynchronous request as the client does, and there is no corresponding function to use the program to hibernate for a certain amount of time, so we can use this method to wait for a certain time to wait for the request to return.
In addition, there are other methods, such as GetOption, SetOption, SetProxy, and so on, these methods are used less, so this is no longer introduced here, need to know friends can consult MSDN.
Next, look at the properties of the ServerXMLHTTP object:
1. onreadystatechange XMLHTTP The callback function when the state of the object changes, this property lays a foundation for the asynchronous operation, allowing the program to know if the XMLHTTP operation is complete without querying the state of the XMLHTTP object.
2. The ReadyState XMLHTTP object state, which has 5 values, from 0 to 4, respectively, representing the meaning:
0-Uninitialized, just using new ActiveXObject ("MSXML". serverxmlhttp.5.0 ") The state at which the object is created
1-load, this time, the open method has been called, but no data has been sent using the Send method
2-already loaded, the Send method has been called to send data, but no response flow is available
3-interacting, receiving data, this time you can use the Responsebody and ResponseText properties to get some of the data you already have.
4-Complete the request, all data has been received complete
Usually, we just need to judge the state 4, at which time the data has been loaded, and the responsebody or ResponseText attributes can be used to obtain the required data.
3. Status HTTP response state code, the normal situation should be 200, if the requested resource does not exist, it will return 404, there are other status code such as server Error 500.
4. StatusText HTTP Response Status text that describes the meaning of the response status code, such as not in ok,404 not Found in OK Found
5. A byte array of responsebody response data, which can be used directly in VBScript, but has to be converted in JScript.
6. ResponseText to obtain response data in text form
7. Responsexml returns the response data as a Xmldom object, which is especially useful when the requested data is an XML document
8. Responsestream response Stream object, this attribute is not commonly used