XML and JSON in Ajax

XML  document

In the process of Ajax request, what is returned is a data format similar to HTML structure, but this is not HTML code but XML format data. Ajax x is XML, which is the format of data transmission defined by Ajax.

  1. XML stands for Extensible Markup Language (EXtensible Markup Language)
  2. XML is a markup language, very similar to HTML , not HTML
  3. XML is designed to transmit data, not display data
  4. XML tags are not predefined. You need to define your own label

Predefined tags and custom tags. Custom tags can name tag names by themselves, and predefine tags such as html

.xml file  

声明头 :<?xml version="1.0" encoding="utf-8" ?>

<?xml version="1.0" encoding="utf-8" ?>
<jibenxinxi>
<xuesheng>
<xingming>小明</xingming>
<xuehao>10001</xuehao>
<xingbie>男</xingbie>
<shengao>180</shengao>
<tizhong>70</tizhong>
</xuesheng>
</jibenxinxi>

Show off

The data returned in the previous case is

<ul>
  <li>学号:10001</li>
  <li>姓名:小明</li>
  <li>性别:男</li>
  <li>年龄:18</li>
  <li>身高:180</li>
  <li>体重:70</li>
</ul>

Use xhr.responseText to receive, because what is returned is an HTML structure wrapped in a string, so innerHTML can be used for splicing, and the HTML structure in the string can be identified in the process

Element.innerHTML = xhr.responseText;  this structure can be rendered in the page at this time

XML drawbacks

If the structure of HTML is returned, the scalability is destined to be poor at this time, because the returned structure does not need to be a fixed structure. For example, what is currently returned to us is a structure of ul and li, but the functions in the page at this time are not applicable to this The structure of ul and li, so we need to expand it ourselves. At this time, we think of the JOSN format

JSON

Comparison of XML and JSON: What XML can do, JSON can also do, and what JSON can do, XML may not do

  • JSON has a richer data structure; XML can only be implemented through tag pairs
  • JOSN has arrays and object settings; and nested with each other, the data can become richer; XML can only be distinguished and nested through tags
  • JSON is lighter, there is no content, but data; XML has redundant tag structure besides data

Question: The data read by Ajax that we are currently learning are all strings, even if the returned JSON is JSON, it will be recognized as a string by xhr.responseText

Method 1 : Use eval() statement, eval() statement can convert a string into a code statement

var data = xhr.responseText;
var obj = eval("("+data+")")

The returned result cannot be directly placed in the eval() function, because eval() essentially converts JavaScript statements, so if we want him to recognize the HTML structure code, we can use parentheses to wrap it.

Method 2 : is to use the built-in constructor Function

var obj = (new Function("return"+xhr.responseText))()

Make a turnaround through the built-in constructor, use the return of the function to remove the string

Method 3: (commonly used) JSON.parse method

var obj = JSON.parse(data)

The longest use is this method, because it is compatible with IE6 and 7, so these compatibility is basically not considered

JOSN.parse() and JSON.stringify() methods are a pair, JSON.parse() converts a string into JSON format, and JSON.stringify() method converts JSON format into a string

 

Guess you like

Origin blog.csdn.net/weixin_41040445/article/details/114968400