AJAX Xmlxmlhttpuest Objects

Source: Internet
Author: User



Xmlxmlhttpuest Object

The main point of AJAX is the Xmlxmlhttpuest object.

using the Xmlxmlhttpuest object object to implement asynchronous communication typically requires the following steps:

(1) Defining Xmlxmlhttpuest Object instances

(2) Call the Open () method to establish a connection to the server

(3) Register the onReadyStateChange event handler function to receive and process information from the server-side response

(4) Call the Send () method to send the request

(1) Instantiating Xmlxmlhttpuest objects

There are differences in the way that different browsers create Xmlxmlhttpuest objects. Internet Explorer uses ActiveXObject, while other browsers use JavaScript built-in objects called Xmlxmlhttpuest.

try{
Firefox, Opera 8.0+, Safari and other non-IE browsers

XMLHttpRequest xmlhttp=new XMLHttpRequest (); }

catch (e) {
Internet Explorer IE browser

try{
Xmlhttp=new ActiveXObject ("msxml2.xmlhttp"); }

catch (e) {try{
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP"); }

catch (e) {
Alert ("Your browser does not support ajax! ");

return false; }      }  }

(2) Establishing a connection

After defining the XMLHttpRequest object, call the open () method to establish an asynchronous connection:

Xmlhttp.open (Method,url,async,user,password)

Method: The way the request is get/post/put/propfind generally used (get/post) ② Open the request, load the data
URL: The requested address, which can be an absolute address or a relative address
Async: For optional, set whether asynchronous communication (true or FALSE)

User and password indicate that the requested server needs to be validated

For example: Xmlhttp.open ("GET", "test.asp", "true");


Xmlhttp.open ("GET", url,true);
Xmlhttp.onreadystatechange=dowork; (DoWork is a JS)
Xmlhttp.send (NULL);

(3) Binding onreadystatechange time processing function

XmlHttp. Onreadystatechange=response;

function response () {

the communication status or Xmlhttp.responsebody,responsestream,responsetext can be tracked through the Xmlhttp.readystate property value, Responsexml and other properties to store different server-side response information

ReadyState Property Value:

0: The request was not initialized (open () has not been called yet).
1: The request has been established but has not yet been sent (send () not yet called)

2: The request has been sent, is in process (usually can now get the content header from the response).

3: The request is in process, and some of the data in the response is usually available, but the server has not finished generating the response.

4: The response is complete; you can get and use the server's response

}

(4) Send the request via the Send () method

Xmlhttp.send (NULL);

A complete communication is:

XMLHttpRequest xmlhttp=new XMLHttpRequest ();

Xmlhttp.open (); Establish a connection

Xmlhttp.onreadystatechange=response; Binding onreadystatechange Events

Xmlhttp.send (NULL); Send Request

alert (xmlHttp.response.responseText);//Prompt server-side corresponding information

AJAX Xmlxmlhttpuest Objects

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.