Use XMLHTTP Request Object to get server data

xiaoxiao2021-03-05  51

Using XMLHTTP objects in the web client, you can use the server to exchange data, we can get and send any type of data, even binary data to the server. XMLHTTP technology is also a way to exchange data for most brushless pages and server exchange data, which is more convenient than ever-hidden iframe.

At the same time, we are happy to be XMLHTTP is not a unique thing, although not currently

W3C standard, but IE, Netscape / Mozilla, and Safari are supported. In IE, we use New ActiveXObject ('msxml2.xmlhtttp') or New ActiveXObject ("Microsoft.xmlhttt (" Microsoft.xmlhtttp ") to use the former or the latter and the MSXML version installed by the client machine. In Netscape / Mozilla and Safari, use New XMLHttpRequest () to get an XMLHTTP object instance. For example, in IE, we usually use this:

VAR

XMLHTTP

=

NULL

;

Try

{XMLHTTP

=

New

ActiveXObject

"

MSXML2.XMLHTTP

"

}

Catch

(e) {

Try

{XMLHTTP

=

New

ActiveXObject

"

Microsoft.xmlhttp

"

}

Catch

(E2) {}}

It is not difficult to use an XMLHTTP object. It has a total of 6 ways 8 properties. However, it is important to provide two execution modes: synchronous mode and asynchronous mode. Synchronous mode can be more accurate control program flow, but if the server's response is too slow, Browser will die the corresponding problem; and use asynchronous mode due to the event trigger mode control process, it will bring some unspeakable expectations to the program. The problem, because you don't know how the client waits for the server Response process, what is doing in Browser. Below is a simple example of acquiring server data in synchronization:

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

'';}}

The properties and method list of XMLHTTP objects (from the IXMLHttpRequest interface):

Name Type Description OnReadyStateChange N / A Specifies the event handler that is called when the ready state changes, only for asynchronous operation ReadyState Long asynchronous operation: Not initialization (0), loaded (1), loaded (2), Interaction (3), completed (4) Responsebody Variant will respond to the text as a UNSIGNED BYTE array Returns the response information The body is the response message to return ResponseText string will respond to the text string to return to ResponseXML Object to return to Responsexml Object response message body parsed returned to XMLDocument object status Long server HTTP status code statusText String server HTTP response line status Name Desciption abort cancel the current HTTP request getAllResponseHeaders retrieve all header fields getResponseHeader from the response information to obtain a HTTP headers from the response message body Head value Open (Method, URL, Boolasync, BStruser, BSTRPASSWORD) Opens a connection Send (Varbody) to set a request for the HTTP server SeetRequestHeader (BSTRHEADER, BSTRVALUE) Send a request to the HTTP server. Can contain the body.

It is obvious that the Open method is more troublesome, bringing a lot of parameters, and their meaning is:

Parameter Description MethodHTTP communication method, such as GET, HEAD, POST, PUT, DELETE, CONNECT, etc. URL address, URL can take QueryStringBoolasync a Boolean identity, indicating whether the request is asynchronous. If it is asynchronous communication, the client will not wait for the server's response; if it is synchronous mode, the client will wait until the server returns the message to perform other operations BStruser user IDs, for server authentication BSTRPASSWORD user password, for server authentication

Example of asynchronous communication:

Xmlhttp.open

"

Get

"

,

"

DEFAULT.ASPX

"

,

True

XMLHTTP.ONREADYSTATECHANGE

=

FUNCTION

() {

IF

(XMLHTTP.ReadyState

==

4

) {Alert (xmlhttp.responsext);}} xmlhttp.send

NULL

);

In fact, using XMLHTTP is as simple, complicated is an organizational mode of server-side data, and developers need developers to be familiar with the development of Client and Server ends. But it seems that there is no relationship between this after a half day, how is it called XMLHTTP? We noticed that there is a responseXML in the response data type, but it parses the returned XMLDocument belongs to the content of XMLDOM, and the relationship with the use of XMLHTTP and the server communication is not big, and later will be detailed later.

转载请注明原文地址:https://www.9cbs.com/read-36600.html

New Post(0)