This article mainly brings you some key knowledge (sharing) that must be mastered in AJAX applications. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
What is AJAX?
is the abbreviation of Asynchronous Javascript And XML. It is not a new language, but a comprehensive utilization of existing technologies. Its essence is to communicate with the server in an asynchronous manner based on the HTTP protocol.
The concept of asynchronous?
means that when a certain program is executed, it will not block the execution of other programs. Its expression is that the execution order of the program does not depend on the writing order of the program itself. On the contrary, it is synchronous.
The role of asynchronous?
The advantage is that it does not block the execution of the program, thereby improving the overall execution efficiency.
The core of AJAX?
XMLHTTPRequest, abbreviation XHR, Xml extensible markup language, Http hypertext transfer protocol, Request request. The XMLHttpRequest object can partially update a web page without submitting the entire page to the server. The XMLHttpRequest object provides full access to the HTTP protocol, including the ability to make POST and HEAD requests as well as ordinary GET requests. XMLHttpRequest can return a Web server's response synchronously or asynchronously, and can return content in the form of text or a DOM document. It can accept any form of text document and is a key feature of AJAX's web application architecture.
As mentioned earlier, XMLHTTPRequest can receive any form of document, so we have to mention two data formats commonly used in network transmission.
Two commonly used data formats xml and JSON?
xml:
The definition of xml: Extensible Markup Language is a markup language used to mark electronic documents to make them structural.
xml specifications:
1. There must be a root element
2. No spaces, no numbers or . at the beginning, case sensitive
3. No cross-embedding Set
4. Attribute double quotes (the browser automatically corrects them into double quotes)
5. Special symbols must use entities
6. Comments are the same as HTML
Although it can describe and transmit complex Data, but its analysis is too complex and large in size, so it is rarely used in implementation development.
Brief description of the steps for using xml:
Since xml is a DOM object, you can directly use the DOM method,
1. var xml=xhr.responseXML; get the XML (given by PHP Interface)
2. var items=xml.querySelector('item'); Get the document structure of xml
3. String splicing
3.1. Initialize var html='';
3.2. Traverse for(){get data var item=item[i],} splice html+='label+data'
4. Render document. querySelector('tbody').innerHTHML=html
JSON:
JSON definition: JavaScript Object Notation, another lightweight text data exchange format, independent of language.
JSON specifications:
1. Data is in name/value pairs
2. Data is separated by commas (the last key/value pair cannot contain a comma)
3. Curly brackets save objects and square brackets save arrays
4. Use double quotes
Cross-language parsing of JSON:
When JSON data is transmitted in different languages, the type is string, different Each language also has its own parsing method, which needs to be parsed before it can be read.
1. PHP parsing method
Convert array to json character json_encode->$json_array = json_encode($array);
Convert json character to array json_decode->$array_json = json_decode($json_array);
2. Javascript parsing method
Convert string to object parse->var jsonObj=JSON.parse(jsonStr);
Convert object to string stringify->var jsonStr=JSON.stringify(jsonObj);
tips: JSON compatible processing refers to event monitoring onreadystatechange in json2.js
XMLHttpRequest?
Related recommendations:
Detailed examples of the load() function of ajax application in jquery
Jquery ajax application example
JavaScript learning summary JS, AJAX application_javascript skills
The above is the detailed content of Important knowledge points must be mastered in AJAX applications. For more information, please follow other related articles on the PHP Chinese website!