How is Ajax specifically used? Four-step parsing using Ajax (with instance)

Source: Internet
Author: User
This article mainly introduces the use of Ajax four steps, want to see the classmate hurriedly look over, now we begin to read this article

1. First step (get XMLHttpRequest)

*ajax actually only need to learn an object: XMLHttpRequest, if mastered it to master the Ajax

* get XMLHttpRequest

> Most browsers support: Var xmlhttp=new XMLHttpRequest ();

>ie6.0 support:var xmlhttp=new ActiveXObject ("msxml2.xmlhttp");

>IE5.5 and earlier versions of IE support:var xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");

* Write a function to create XMLHttpRequest

function Createxmlhttprequest () {           try{                    return  new  XMLHttpRequest ();           } catch{                    try{                            return  new  ActiveXObject ("msxml2.xmlhttp");                    } catch{                                try{                                        return  newactivexobject ("Microsoft.XMLHTTP");                             } catch{                                  Alert ("How possible, what browser do you use ... ")                                   throw   e;                             }                    }           }

2. Step two (open the connection to the server)

*xmlhttp.open (); To open a connection to the server, three parameters are required;

> Request method: Get or Post

> Requested URL: Specify server-side resources such as:/Project/action

> Whether the request is asynchronous: If the asynchronous request is sent as true, otherwise it is a synchronous request;

*xmlhttp.open ("GET", "/Project/action", true); (Want to see more on the Topic.alibabacloud.comAJAX Development Manual section of the study)

3. Step three (send request)

*xmlhttp.send (NULL), if not NULL in parentheses may cause some browsers to fail to send;

> Parameter: Is the content of the request body, if it is a GET request, must give NULL

4. Fourth Step

* Register Listener on an event of the XMLHTTP object: Onredaystatechange

*There are 5 states for XMLHTTP objects:

>0 status: Just created, not yet called the Open () method;

>1 Status: Request started, call the Open () method, but have not yet called the Send () method;

>2 State: The Send () method is finished;

>3 Status: The server has started responding, but does not indicate the end of the response;

>4 Status: Server response is over! (we usually only focus on this state!!!) )

* Get the state of the XMLHTTP object;

>var state =xmlhttp.redaystate;//May be 0, 1, 2, 3, 4

* Get the response status code of the server

>var state = xmlhttp.status;//For example 200, 404, +

* Get the server response content

>var  content = Xmlhttp.responsetext;//Receive the response of the server in text format;      >var contents  = xmlhttp.responsexml;// Gets the XML content of the server response, which is the document object;  xmlhttp.onredaystatechange=function () {//5 states call this method;           Xmlhttp.redaystate==4 && xmlhttp.status==200) {  //double judgment, integral                //Get server response content                  var  text = Xmlhttp.responsetext;           } };

This is the end of this article (want to see more on the Topic.alibabacloud.comAJAX User manual section of the study), there are questions can be in the bottom of the message to ask questions.

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.