Getting server data using XMLHTTP Request object

Source: Internet
Author: User
object|request|xml| Server | data

Using the XMLHTTP object on a Web client makes it easy to exchange data with the server, and we can get and send any type of data, even binary data, to the server. XMLHTTP technology is also the most current no Refresh page use and server Exchange data, this way than the previous method of hiding the iframe more convenient and more economical.

At the same time, let's be happy that XMLHTTP is not something that is unique to IE, although it is not the standard of the current consortium, but IE, Netscape/mozilla, and safari support it. In IE we use the new ActiveXObject (' MSXML2. XMLHTTP ') or the new ActiveXObject ("Microsoft.XMLHTTP") to obtain an instance of XMLHTTP object, using the former or the latter and the version of MSXML installed by the client machine. In Netscape/mozilla and Safari, use the new XMLHttpRequest () to get the 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) {}
}


Using a XMLHTTP object is really not a difficult thing, it's a total of 6 methods 8 properties. But the main thing is to provide two modes of execution: Synchronous mode and asynchronous mode. Synchronization mode can be more accurate program flow, but if the server's response too slow, browser will have lost the corresponding problem, and the use of asynchronous mode because it is the event trigger to control the process, will bring some unpredictable problems to the program operation, Because you do not know the client waits for the server to response the process, the user will do in browser what operation.

The following is a simple example of how to obtain server data synchronously:

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 ";
}
}

List of properties and methods for the XMLHTTP object:

Name Type Description
onReadyStateChange N/A Specifies the event handler function that is invoked when the ready state changes, only for asynchronous operations
ReadyState Long Status of the asynchronous operation: uninitialized (0), loading (1), Loaded (2), Interaction (3), completed (4)
Responsebody Variant Returns the body of the response message as a unsigned byte array
Responsestream Variant Returns the body of the response message as an ADO Stream object
ResponseText String Returns the body of the response message as a text string
Responsexml Object Resolves the body of the response message to a XmlDocument object through XMLDOM
Status Long HTTP status code returned by the server
StatusText String Server HTTP response row status
Name < Strong>desciption
Cancel the current HTTP request
retrieves all header fields from the response information
get an HTTP header value from the body of the response message
open (method, URL, Boolasync, Bstruser, Bstrpa ssWOrd)
s End (varbody)

This is obviously a problem with the open method, with a lot of parameters, and they mean:
Parameter Description
method http means of communication, such as GET, head, POST, put, DELETE, connect, etc.
url URL of the server that receives the data, the URL can be brought querystring
boolasync A Boolean ID, description please Asks whether it is asynchronous. In the case of asynchronous communication, the client does not wait for a response from the server; if it is synchronized, the client waits until the server returns the message to perform other actions
bstruse R user ID, for server Authentication
bs Trpassword user password, for server Authentication

Example of asynchronous communication:

Xmlhttp.open ("Get", "default.aspx", true);
Xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readystate==4)
{
alert (Xmlhttp.responsetext);
}
}
Xmlhttp.send (NULL);


In fact, the use of XMLHTTP is so simple, complex is the server-side data organization, and requires developers to be familiar with the client and server-side development, in order to be more effective. But as if to say that this thing and XML have no relationship ah, how to call XMLHTTP? We notice that there is a responsexml in the response data type, but it resolves that the returned XmlDocument belong to XMLDOM, and that the relationship between using XMLHTTP to communicate with the server is not large, and then come to the details later.



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.