Basic server XMLHTTP (server XMLHTTP in ASP) Knowledge

Source: Internet
Author: User

Compared with Ajax, XMLHTTP on the server uses the XMLHTTPRequest object on the server. Although it is inconvenient to use asynchronous requests on the server side, it is no harm to learn it as a component that can send HTTP requests on the server side.
Here, I am talking about using XMLHttpRequest on the server side in the ASP environment and using JScript as a demonstration Code Therefore, you need to understand ASP and JScript.
XMLHTTP on the server side is usually used to obtain webpage or other content on the remote host. The news aggregation system generally uses the XMLHTTP object on the server side to obtain the content of the feed to be aggregated, then, the xmldom object is used to analyze the feed content, retrieve the news title, author, content, and other information, and then store the news from several data sources in the database. It 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, which is the basis for all operations on the server. Copy code The Code is as follows: var XMLHTTP = new activexobject ("msxml2.serverxmlhttp. 5.0 ");

First, let's take a look at the serverxmlhttp object's useful methods:

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 at a certain time, you can use this method to cancel the request.

2. the return value of the getAllResponseHeaders method is a string, which is equivalent to removing the request method, Uri, and Protocol version information from the HTTP request header.

3. The getResponseHeader method is used to obtain the information of the specified header. It is useful to obtain the Content-Type and Referer of the returned data.

4. Open initializes a request using the specified request method, Uri, synchronization method, and authentication information.

5. send sends an HTTP request and waits for receiving the response data. Note that if a request is sent synchronously, The send method will not return immediately after the call, but will not return until the request is complete, when an asynchronous request is made, the request is immediately returned. In addition, the send method has an optional parameter body, indicating the data to be sent, which is useful when using the POST method.

6. setTimeout: set four timeout times for the serverxmlhttp object: domain name resolution, connection server, data sending, and response receiving. You can set the corresponding timeout time to control the serverxmlhttp object so that serverxmlhttp cannot be returned in a timely manner.ProgramStop the response.

7. setRequestHeader: Set the request header. In the XMLHttpRequest client, it is usually used to set the request data type or identify the request method. For example, jquery adds the header id x-request-, the request is sent from the XMLHTTPRequest object to facilitate the server to make corresponding actions.

8. When waitforresponse sends a request asynchronously, you can use this method to control the request process. In server-side scripts, callback functions cannot be directly used to control asynchronous requests as the client does, and no corresponding functions can be used to sleep the program 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, and setproxy. These methods are rarely used, so we will not introduce them here. If you need to know more, you can refer to msdn.

Next, let's take a look at the attributes of the serverxmlhttp object:

1. onreadystatechange: the callback function when the XMLHTTP object state changes. This attribute lays a foundation for asynchronous operations, so that the program can know whether the XMLHTTP operation has been completed without querying the status of the XMLHTTP object.

2. readystate XMLHTTP object status, which has five values, from 0 to 4, respectively indicates:
0-not initialized, just used new activexobject ("MSXML. serverxmlhttp.5.0 ") Status of the object during creation
1-loading. At this time, the open method has been called, however, data has not been sent using the send method
2-it has been loaded and the send method has been called to send data, but no response stream is available
3-interacting, receiving data. At this time, you can use the responsebody and responsetext attributes to obtain the obtained data.
4. The request is completed. All data has been received.
generally, we only need to judge status 4. At this time, all the data has been loaded, and the required data can be obtained using the responsebody or responsetext attribute..
3. Status HTTP response status code. The normal condition is 200. If the requested resource does not exist, 404 is returned, and other status codes such as server error 500 are returned.
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 responds to the byte array of data, which can be directly used in VBScript, but must be converted in JScript.
6. responsetext obtains response data in text format
7. responsexml returns the response data as an xmldom object, which is particularly useful when the requested data is an XML document.
8. responsestream responds to the stream object, which is not commonly used

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.