Ajax learning notes sorting, ajax learning notes

Source: Internet
Author: User

Ajax learning notes sorting, ajax learning notes

Ajax: Asynchronous JavaScript and Xml, Asynchronous js scripts and xml, which are often used to implement partial Asynchronous page refresh, which is of great help to improve user experience. xml is advantageous in multiple languages, but Ajax uses Json objects rather than Xml to process data.

Ajax history... understanding knowledge

Ajax belongs to Web Front-end development technology and is closely related to javascript exceptions. ajax is a new technology that implements asynchronous communication without refreshing, and this technology can be implemented in many ways. netScape, the originator of the browser, first invented the LiveScript script language to enrich the forms of web elements, so that web pages can present dynamic results. after subsequent revisions and upgrades, the JavaScript language was born. In the same period, Microsoft also saw the prospect of the Internet and began to get involved in the Internet industry and launched the JScript Language, it is a pity that JavaScript is not mature and development lags behind. microsoft's determination to the Internet eventually led to MS's long and tortuous acquisition history of NS.

The Dynamic HTML Language (Dynamic Hyper Text Markup Language) is to place javascript in the element node of the Dom tree to provide Dynamic display behavior for elements.

Ajax code ideas

CREATE request object

Configure the request object and send it to the server

Server Response Request object

(1) create a request object to communicate with the server:

function createRequest(){try{var request=new XMLHttpRequest();}catch(tryMS){try{request=new ActiveXObject("Msxml2.XMLHTTP");}catch(otherMS){try{request=new ActiveXObject("Microsoft.XMLHTTP");}catch(failed){request=null;}}}return request;}

Note: ActiveXObject is a Microsoft-specific programming object. Two different versions are supported by different browsers. XMLHTTP is a set of APIs that can transmit or receive XML and other data in scripting languages such as Javascript over http.

After obtaining the request object request, it has the following attributes:

Commonly used include onreadystatechange, readyState, responseText, and status.

(2) configure the request object and send it to the server:

var request=createRequest();request.open("GET",url,true);request.onreadystatechange=showResponse;request.send(null);

The open and send methods all come from the XMLHttpRequest prototype of the request object. open _ proto __: XMLHttpRequest and you can see:

The open () method has three parameters. The first one is the method used to send the request (GET or POST, and the difference will be summarized later ), the second parameter specifies the url of the server script (the file can be of any type), and the third parameter specifies whether asynchronous processing is performed (true asynchronous by default)

The send () method sends the request to the server.

I noticed here that the request. onreadystatechange = showResponse; Code is located in different locations, and the console outputs different response content. The time when the callback function is added to the task queue is different, for example, the current position,

Request. onreadystatechange = showResponse; before sending after open, when the code is parsed to request. onreadystatechange = showResponse; at this time, readyStates = 1 (the request has been established with the server). Then, when readyStates changes from 1 to 2, the callback function showResponse is added to the task queue for execution, the readyState changes from 2 to 3, the callback function showResponse joins the task queue for the second time, the readyState changes from 3 to 4, and the callback function joins the queue again. So it is assumed that after the main code is executed, the console will output "the request has been sent, the server has received, after sending", "the request is being processed", and "The request has been completed"; indeed:

(3) when the server responds to the request object, js can process the response content:

Function showResponse () {if (request. readyState = 0) {console. log ('request not initialized, before calling open ');} else if (request. readyState = 1) {console. log ('request submitted, server connection established, before open ');} else if (request. readyState = 2) {console. log ('request sent, server received, after send');} else if (request. readyState = 3) {console. log ('request processing ');} else if (request. readyState = 4) {console. log ('request completed '); if (request. status = 200) {// process the request. responseText ;}}}

The above code is only used to track readyState changes, so the actual project code does not need so much. Below is the actual main code:

Function showResponse () {if (request. readyState = 4) {if (request. status = 200) {// process request. responseText ;}}}

Ps: What issues does Ajax solve?

We all know that when the client requests a page from the server, the server first dynamically calculates and generates the page, and then sends it to the client. The client browser compiles and presents the page in sequence.

If there is no Ajax: if there is a user verification control on the page, when the client browser displays the user verification control, it will wait for the server's verification results to continue to render the page elements after receiving the results. this verification process usually requires database reading and other operations. This is the so-called synchronous method. in this way, the webpage may be displayed in a false state.

After Ajax is used: it is also a verification control. After the client submits the verification request, it continues to present other elements in sequence. after the verification result is obtained, javascript modifies the DOM object in the memory on the client and presents it to the user (Note: Only the DOM object in the memory is modified here, the page file received by the client is not modified ). in this way, the asynchronous mode will not be suspended, and the client also saves time overhead when waiting for the server to return results.

Articles you may be interested in:
  • Unveil the secrets of AJAX (AJAX personal study notes)
  • AJax study note 2 (Role of onreadystatechange)
  • JavaScript advanced programming XML and Ajax learning notes
  • Ajax learning Note 1 in jquery
  • Jquery ajax learning notes 2 use the responseXML of the XMLHttpRequest object
  • Ajax study notes in jquery 3
  • Ajax study notes in jquery 4
  • JQuery learning notes-Ajax operations (I)-Data Loading
  • JQuery learning notes-Ajax operations (II)-Data Transmission
  • JQuery learning notes-Ajax operations (III)-process Processing

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.