Msxml2.xmlhttp Microsoft. XMLHTTP new XMLHttpRequest

Source: Internet
Author: User

The client calls XMLHTTP in five steps:
1. Create an XMLHTTP object
2. Open the connection with the server, and define the command sending method, Service webpage (URL), and request permissions.
The client opens a connection to the Service webpage of the server through the open command. Like normal HTTP Command Transmission, you can use the "get" method or "Post" method to direct to the Service webpage of the server.
3. Send commands.
4. Wait for and receive the processing results returned by the server.
5. Release the XMLHTTP object

Define an XMLHTTP object:

Set objxml = Createobject (msxml2.xmlhttp) or
Set objxml = Createobject ("Microsoft. XMLHTTP") (vbs)
VaR xml = new activexobject ("Microsoft. XMLHTTP" (JavaScript)
'Or, for version 3.0 of XMLHTTP, use:
Set xml = server. Createobject (msxml2.serverxmlhttp)

XMLHTTP method:

Open (bstrmethod, bstrurl, varasync, bstruser, bstrpassword)
Bstrmethod: data transmission method, that is, get or post. Use the "post" method to send data, which can be 4 MB in size or "get", and can only be kb.
Bstrurl: the URL of the Service webpage.
Varasync: async: A boolean identifier indicating whether the request is asynchronous. If it is an asynchronous communication method (true), the client will not wait for the server's response; if it is a synchronous mode (false), the client will wait until the server returns a message to execute other operations

Bstruser: user name, which can be omitted.
Bstrpassword: user password, which can be omitted.

Send (varbody)
Varbody: instruction set. It can be XML data, a string, a stream, or an unsigned integer array. It can also be omitted, so that the command can be substituted by the URL parameter of the open method. Data transmission methods can be synchronous or asynchronous. In asynchronous mode, once a packet is sent, the send process is terminated and the client performs other operations. In synchronous mode, the client waits until the server returns a confirmation message to terminate the send process.

SetRequestHeader (bstrheader, bstrvalue)
Bstrheader: HTTP Header)
Bstrvalue: HTTP header value
If the open method is defined as post, you can define form upload:
XMLHTTP. setRequestHeader "Content-Type", "application/X-WWW-form-urlencoded"

Abort
Cancel current HTTP Request

GetAllResponseHeaders
Retrieve all header fields from the response information

GetResponseHeader
Obtain an HTTP header value from the response body.

Iii. XMLHTTP attributes:

Onreadystatechange

Specifies the event handling handle when the readystate attribute changes.

Syntax

 
Oxmlhttprequest. onreadystatechange = funcmyhandler;

Example

The following example shows how to call the handlestatechange function when the readystate attribute of the XMLHTTPRequest object is changed. After the data is received (readystate = 4), a button on this page will be activated.


Function getrequest ()
{
 
VaR XMLHTTP = false;

Try {
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
} Catch (E2 ){
XMLHTTP = false;
}
}

If (! XMLHTTP & typeof XMLHttpRequest! = 'Undefined '){
XMLHTTP = new XMLHttpRequest ();
}

Return XMLHTTP;
}
Function dealwithdata (requesttype, async, requesturl, senddata, updatemethod)
{
VaR XMLHTTP = getrequest ();
XMLHTTP. Open (requesttype, requesturl, async );
If (requesttype. touppercase () = "Post ")
XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");

XMLHTTP. onreadystatechange = updatemethod;
If (senddata = '')
Senddata = NULL;
XMLHTTP. Send (senddata );
}

Remarks

This attribute is only written and is an extension of the W3C Document Object Model.

Readystate
The readystate attribute in the XMLHTTP object can reflect the progress of the server when processing requests. ClientProgramYou can set the corresponding Event Processing Method Based on the status information. The attribute values and their meanings are shown in the following table:
Value description
0 response object has been created, but the XML Document Upload process has not been completed
1. the XML document has been loaded.
2. the XML file has been loaded and is being processed.
3. Some XML documents have been parsed.
4. The file has been parsed and the client can accept the returned message.

Responsebody
Returns the result of the variant type as an unsigned integer array.

Responsestream
Variant results are returned as istream streams.

Responsetext

Returns response information as a string.

Syntax

 
Strvalue = oxmlhttprequest. responsetext;

Example

 
VaR XMLHTTP = new activexobject ("msxml2.xmlhttp. 3.0 "); XMLHTTP. open ("get", "http: // localhost/books. XML ", false); XMLHTTP. send (); alert (XMLHTTP. responsetext );

Remarks

Variable, which is read-only and returns response information as a string.
XMLHTTP tries to decode the response information as a unicode string, and XMLHTTP sets the encoding of the response data as a UTF-8 by default. If the data returned by the server includes BOM (byte-order mark ), XMLHTTP can decode any UCS-2 (big or little endian) or UCS-4 data. Note: If the server returns an XML document, this attribute does not process the encoding declaration in the XML document. You need to use responsexml for processing

Responsexml
The object type results are returned in XML format.

Status
HTTP status code returned by Long Server

Statustext
String Server HTTP Response line status

Appendix

(1) status supported by HTTP 1.1Code

100 the initial request for continue has been accepted, and the customer shall continue to send the rest of the request
101 The switching protocols server converts a client-compliant request to another protocol
200 OK everything is normal, and the response document to the get and post requests follows.
The 201 created server has created a document and the location header provides its URL.
202 accepted has accepted the request, but the processing has not been completed.
203 the non-authoritative information document has been normally returned, but some response headers may be incorrect because the document copy is used.
204 NO content does not have a new document. The browser should continue to display the original document. This status code is useful if the user regularly refreshes the page and the servlet can determine that the user document is new enough.
205 there is no new content in the reset content, but the browser should reset the content displayed by it. Used to force the browser to clear the input content of the form
206 partial content the client sent a GET request with the range header, and the server completed it.
300 the documents requested by the multiple choices client can be found in multiple locations, which are listed in the returned documents. If the server needs to give priority, it should be specified in the location response header.
301 moved permanently the document requested by the customer is elsewhere. The new URL is provided in the location header and the browser should automatically access the new URL.
302 found is similar to 301, but the new URL should be treated as a temporary alternative, rather than permanent.
303 see other is similar to 301/302. The difference is that if the original request is post, the redirection target document specified by the location header should be extracted through get
304 The not modified client has a buffered document and issued a conditional request (generally, the IF-modified-since header is provided to indicate that the customer only wants to update the document on a specified date ). The server tells the customer that the original buffer documentation can still be used.
305 the document requested by the use proxy client should be extracted from the proxy server specified by the location header
307 temporary redirect and 302 (found) are the same. Many browsers mistakenly respond to the 302 response for redirection. Even if the original request is post, it can only be redirected when the POST request actually responds to 303. For this reason, HTTP 1.1 adds 307 to clear the region code in several states: When a 303 response occurs, the browser can follow the redirected get and post requests; if a 307 response occurs, the browser can only follow the redirection to get requests.
400 syntax error in bad request.
401 unauthorized the customer attempted to access the password-protected page without authorization. The response contains a WWW-Authenticate header. the browser displays the username/password dialog box accordingly, and then sends a request again after entering the appropriate authorization header.
403 Forbidden resources are unavailable.
404 Not found cannot find the resource at the specified position
405 method not allowed request methods (get, post, Head, delete, put, Trace, etc.) are not applicable to specified resources.
406 the resource specified by not acceptable has been found, but its MIME type is incompatible with the one specified by the customer in the accpet header.
407 proxy authentication required is similar to 401, indicating that the customer must first be authorized by the proxy server.
408 request timeout the client has not sent any request within the waiting time permitted by the server. The customer can repeat the same request later.
409 conflict is usually related to put requests. The request cannot be successful because the request conflicts with the current status of the resource.
410 the document requested by gone is no longer available, and the server does not know which address to redirect. It differs from 404 in that if 407 is returned, the document permanently leaves the specified position, and 404 indicates that the document is unavailable for unknown reasons.
The 411 length required server cannot process the request unless the customer sends a Content-Length header.
412 precondition failed: Some of the prerequisites specified in the request header fail.
413 the size of the target Request Entity too large document exceeds the size that the server is willing to process. If the server thinks it can process the request later, it should provide a retry-after Header
414 request URI Too long URI is too long
416 the requested range not satisfiable server cannot meet the range header specified by the customer in the request.
500 the internal server error server encounters unexpected circumstances and cannot complete the customer's request
501 The not implemented server does not support the functions required to implement the request. For example, the customer sends a put request not supported by the server.
502 when the Bad Gateway server acts as a gateway or proxy, in order to complete the request to access the next server, but the server returns an INVALID RESPONSE
503 the service unavailable server fails to respond due to maintenance or overload. For example, Servlet may return 503 when the database connection pool is full. A retry-after header can be provided when the server returns 503
504 gateway timeout is used by a proxy or gateway server, indicating that the remote server cannot receive a response in a timely manner.
505 the HTTP Version Not Supported server does not support the HTTP Version specified in the request

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.