Key knowledge (Sharing) That must be mastered in AJAX applications
What Is AJAX?
It is the abbreviation of Asynchronous Javascript And XML. It is not a new language, but a comprehensive use of existing technologies. In essence, HTTP is used to communicate with the server asynchronously.
What is asynchronous?
When a program is executed, it does not block other programs. Its execution order is not dependent on the writing order of the program. On the contrary, it is synchronous.
What is the role of Asynchronization?
Its advantage is that it does not block the execution of programs, thus improving the overall execution efficiency.
What is the core of AJAX?
XMLHTTPRequest, abbreviated as XHR, Xml eXtensible Markup Language, Http Hypertext Transfer protocol, and Request. The XMLHttpRequest object can be used to update webpages 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 and common GET requests. XMLHttpRequest can synchronously or asynchronously return the response of the Web server and return the content in the form of text or a DOM document. It can receive text documents of any form and is a key feature of AJAX Web application architecture.
As mentioned above, XMLHTTPRequest can receive documents of any form, so we have to mention two common data formats used in network transmission.
What are two common data formats: xml and JSON?
Xml:
Xml definition: Extensible Markup Language (Markup Language) is a structured markup language used to mark electronic files.
Xml specifications:
1. A root element is required.
2. It cannot contain spaces, numbers, or. prefixes, and is case sensitive.
3. Nesting is not allowed.
4. Attribute double quotation marks (the browser automatically changes to double quotation marks)
5. Use objects for special symbols
6. Comments are the same as HTML
Although it can describe and transmit complex data, the parsing is too complicated and the volume is large, so implementation development is rarely used.
Xml usage steps:
Because xml is a DOM object, you can directly use the DOM method,
1. var xml = xhr. responseXML; get XML (PHP Interface)
2. var items = xml. querySelector ('item'); get the xml document structure
3. String concatenation
3.1 initialize var html = '';
3.2 traverse for () {obtain data var item = item [I],} splice html + = 'tag + data'
4. Render document. querySelector ('tbody'). innerHTHML = html
JSON:
JSON definition: JavaScript Object Notation, another lightweight text data exchange format, independent of languages.
JSON specifications:
1. Data in name/value pairs
2. Data is separated by commas (,) (the last key/value pair cannot contain commas)
3. Brackets save object square brackets Save Array
4. Double quotation marks
JSON cross-language parsing:
When JSON data is transmitted in different languages, the data type is string. Different languages also have corresponding parsing methods, which can be read only after the parsing is complete.
1. PHP Parsing Method
Array to json character json_encode-> $ json_array = json_encode ($ array );
Convert json characters into arrays json_decode-> $ array_json = json_decode ($ json_array );
2. Javascript Parsing Method
String to object parse-> var jsonObj = JSON. parse (jsonStr );
Object To String stringify-> var jsonStr = JSON. stringify (jsonObj );
Tips: JSON compatible processing references json2.js
Event listening in XMLHttpRequest onreadystatechange?
The key knowledge (SHARE) that must be mastered in the above AJAX application is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for the help house.