Basic development of Ajax you know what? An explanation of the basic development steps of Ajax (with examples in it)

Source: Internet
Author: User

This article is mainly about the introduction of the basic development of Ajax, want to know more about the development of Ajax will have to develop steps, although the most basic, but also can let people know how to develop with Ajax. Start reading this article now.

    1. What is Ajax? What's the use?
      The Ajax full name is "asynchrous (asynchronous) Javascript and XML", which is a web development technique that creates an interactive Web application. is a fast-moving web technology that is used to improve the efficiency of browser and server interaction. The ability to replace traditional traditional web interaction patterns.

    2. How to understand this synchronous asynchronous?

      What is the traditional synchronous interaction mode?
      In the traditional model, the synchronous interaction mode is used, the client browser makes a request to the server, and then waits until the server responds to the request, and cannot do anything else.
      What is the main technology of asynchronous interactive mode?
      In the Ajax asynchronous interactive mode, in the client browser and server, there is a passing object Ajax engine, when the client browser is to submit a request to the Ajax engine, and then by the Ajax engine and the server to request information transfer, so that the client browser to send the request, Be able to continue the operation, rather than being in a wait state. When the server finishes processing the AJAX engine request, the response is passed to the Ajax engine, and the AJAX engine updates the client browser page.

    3. Ajax Development Steps
      First step: Get the XMLHttpRequest object.
      The sample code can be found in the W3school XML document.

var xmlhttp = null;if (window. XMLHttpRequest) {    //This is for browser IE7 and its version 7 or more.    xmlhttp = new XMLHttpRequest ();} else if (window. ActiveXObject) {    //This is for browsers 5 to 7 versions.    xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");}

Step Two: Register the callback function.

Xmlhttp.onreadystatechange = function () {};

onReadyStateChange is an event handler that is used for events that are triggered by each state change.

Step Three: Open ("Method", "URL", Async, "uname", "pswd")
It is used only to set the request mode, URL path, and synchronization, and does not send the request.

Parameters function
Method The optional parameters for sending the request are: GET, post, and put.
Url To send the URL path, you can use an absolute path and a relative path.
Async Whether the request is to be processed asynchronously. You can also not write, the default is true.
True, script processing will continue processing after send () is sent, that is, asynchronous processing.
False, the script waits for a response before proceeding with the script processing, that is, synchronous processing.
Uname It's not working at the moment, and the documentation doesn't explain. Just don't write it right.
pswd It's not working at the moment, and the documentation doesn't explain. Just don't write it right.


What if the URL requires parameters?
If it is a GET request, the parameters are stitched directly behind the URL.
For example: Xmlhttp.open ("Get", "Http://www.baidu.com?name=xiaosan");

If this is a POST request, you need to do multiple setup operations:
For example: Xmlhttp.open ("Post", "http://www.baidu.com");
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send ("Name=xiaosan");
Where the function of setRequestHeader ("label", "value") is to add the lable/value pair to the HTTP header that you want to send.

Fourth step: Send (content)
Send the request. If there are no arguments, it is written as send (null).

Fifth step: Perform the specific data operation in the callback function.
Before you do this, you should understand some of the properties of XMLHttpRequest.

Properties Description
onReadyStateChange When the state changes, some things can be triggered.
ReadyState See the table below for details
Status Number return status, such as "OK" for the "404", "Not Found"
StatusText String return status, such as "OK", "not Found", etc.
ResponseText Returns the response as a string.
Responsexml Returns the response as XML. Returns an XML object that can be parsed with the DOM

The following table is a readystate state value and the meaning that the status value represents:

Status Value status explanation of the Representative
0 Uninitialized state, which represents the XMLHttpRequest object before it is created
1 The state in the load, open operation.
2 The state that was loaded, the send operation.
3 The state of the interaction, received the response data, but only the response header, the body has not yet received.
4 The completed status, all HTTP responses received are completed.

    1. What is Ajax? What's the use?
      The Ajax full name is "asynchrous (asynchronous) Javascript and XML", which is a web development technique that creates an interactive Web application. is a fast-moving web technology that is used to improve the efficiency of browser and server interaction. The ability to replace traditional traditional web interaction patterns.

    2. How to understand this synchronous asynchronous?

      What is the traditional synchronous interaction mode?
      In the traditional model, the synchronous interaction mode is used, the client browser makes a request to the server, and then waits until the server responds to the request, and cannot do anything else. (Want to see more on the Topic.alibabacloud.comAJAX Development Manual section of the study)
      What is the main technology of asynchronous interactive mode?
      In the Ajax asynchronous interactive mode, in the client browser and server, there is a passing object Ajax engine, when the client browser is to submit a request to the Ajax engine, and then by the Ajax engine and the server to request information transfer, so that the client browser to send the request, Be able to continue the operation, rather than being in a wait state. When the server finishes processing the AJAX engine request, the response is passed to the Ajax engine, and the AJAX engine updates the client browser page.

    3. Ajax Development Steps
      First step: Get the XMLHttpRequest object.
      The sample code can be found in the W3school XML document.

var xmlhttp = null;if (window. XMLHttpRequest) {    //This is for browser IE7 and its version 7 or more.    xmlhttp = new XMLHttpRequest ();} else if (window. ActiveXObject) {    //This is for browsers 5 to 7 versions.    xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");}

Step Two: Register the callback function.
Xmlhttp.onreadystatechange = function () {
};
onReadyStateChange is an event handler that is used for events that are triggered by each state change.

Step Three: Open ("Method", "URL", Async, "uname", "pswd")
It is used only to set the request mode, URL path, and synchronization, and does not send the request.

Parameters function
Method The optional parameters for sending the request are: GET, post, and put.
Url To send the URL path, you can use an absolute path and a relative path.
Async Whether the request is to be processed asynchronously. You can also not write, the default is true.
True, script processing will continue processing after send () is sent, that is, asynchronous processing.
False, the script waits for a response before proceeding with the script processing, that is, synchronous processing.
Uname It's not working at the moment, and the documentation doesn't explain. Just don't write it right.
pswd It's not working at the moment, and the documentation doesn't explain. Just don't write it right.


What if the URL requires parameters?
If it is a GET request, the parameters are stitched directly behind the URL.
For example: Xmlhttp.open ("Get", "Http://www.baidu.com?name=xiaosan");

If this is a POST request, you need to do multiple setup operations:
For example: Xmlhttp.open ("Post", "http://www.baidu.com");
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send ("Name=xiaosan");
Where the function of setRequestHeader ("label", "value") is to add the lable/value pair to the HTTP header that you want to send.

Fourth step: Send (content)
Send the request. If there are no arguments, it is written as send (null).

Fifth step: Perform the specific data operation in the callback function.
Before you do this, you should understand some of the properties of XMLHttpRequest.

Properties Description
onReadyStateChange When the state changes, some things can be triggered.
ReadyState See the table below for details
Status Number return status, such as "OK" for the "404", "Not Found"
StatusText String return status, such as "OK", "not Found", etc.
ResponseText Returns the response as a string.
Responsexml Returns the response as XML. Returns an XML object that can be parsed with the DOM

The following table is a readystate state value and the meaning that the status value represents:

Status Value status explanation of the Representative
0 Uninitialized state, which represents the XMLHttpRequest object before it is created
1 The state in the load, open operation.
2 The state that was loaded, the send operation.
3 The state of the interaction, received the response data, but only the response header, the body has not yet received.
4 The completed status, all HTTP responses received are completed.

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 message below the question.

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.