Ajax learning notes-Ajax Data Format

Source: Internet
Author: User

Html

HTML consists of some common text. If the server sends HTML via XMLHttpRequest, the text is stored in the responsetext attribute. You do not have to read data from the responsetext attribute. It is already in the expected format and can be directly inserted into the page. The easiest way to insert HTML code is to update the innerthml attribute of this element.

<! Doctype HTML> 

A.html

<a href="http://www.baidu.com">http://www.baidu.com</a>

Advantages

-The HTML code sent from the server does not need to be parsed using JavaScript on the browser.

-Good HTML readability

-The combination of HTML code blocks and innerhtml attributes is highly efficient.

Disadvantages

-If you need to update multiple parts of a document through Ajax, HTML is not suitable.

-Innerhtml is not Dom Standard

XML

<! Doctype HTML> 

Andy. xml

<?xml version="1.0" encoding="UTF-8"?><details><name>Andy Budd</name><website>http://www.baidu.com</website><email>[email protected]</email></details>

Advantages

-XML is a common data format.

-You do not have to add the data to the predefined format. Instead, you need to customize a proper tag for the data.

-Use Dom to have full control over documents

Disadvantages

-If the document is from the server, ensure that the document contains the correct header information. If the document type is incorrect, the value of responsexml will be empty.

-Dom parsing may be complicated when the browser receives a long XML file


JSON

JSON (JavaScript Object Notation) is a simple data format, which is lighter than XML and is a javascript native format. This means that no special API or toolkit is required to process JSON in JavaScript.

<SCRIPT type = "text/JavaScript"> var object = {"name": "umgsai", "Age": 12, "Address": {"city ": "Beijing", "school": "ctgu"}, "Teaching": function () {alert ("this is a method") ;}}; alert (object. name); alert (object. age); alert (object. address. city); object. teaching ();/* use a string as a statement to execute var teststr = "alert ('hello')"; EVAL (teststr ); * // * var jsonstr = "{'name': 'umgsai '}"; var testobject = eval ("(" + jsonstr + ")"); // convert the string to the JSON object alert (testobject. name); */</SCRIPT>
<! Doctype HTML> 

Andy. JSON

{"person":{"name":"umgsai","website":"http://www.baidu.com","email":"[email protected]"}}

Advantages

-As a data transmission format, JSON is similar to XML, but JSON is more flexible.

-JSON does not need to send header information containing specific content types from the server.

Disadvantages

-Syntax is too rigorous

-The code is not easy to read.

-Risks of eval Functions

Summary

-If the application does not need to share data with other applications, it is easiest to use HTML fragments to return data.

-If data needs to be reused, JSON files are a good choice and have advantages in performance and size.

-When remote applications are unknown, XML documents are the first choice, Because XML is the "World Language" in the Web service field"

This article is from the "Avatar" blog, please be sure to keep this source http://shamrock.blog.51cto.com/2079212/1561511

Ajax learning notes-Ajax Data Format

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.