In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to achieve data transfer in Ajax", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve data transfer in Ajax" this article.
Ajax is the acronym for "asynchronous javascript and XML", but while XML is a seemingly important part, it is not necessary. Douglas Crock ford, a veteran software engineer, has developed a data format built into javascript, called javascript object representation (JSON,javascript Object Notation), which means to use Ajax objects to convey information directly, which can be pronounced "Jason".
1. What is JSON?
The concept of JSON is simple. JSON is a lightweight data format based on a subset of javascript syntax, namely arrays and object representations. Because you are using the javascript syntax, the JSON definition can be included in the javascript file, and access to it does not require additional parsing through the XML-based language. Before using JSON, however, it is important to understand the special syntax of arrays and object literals in javascript.
1.1 Array literals
Array literals, which enclose a set of javascript values separated by commas in square brackets, for example:
The code is as follows:
Var aNames= ["hello", 12, true, null]
[html]
1.2 object literals
The literal amount of the object is defined by two curly braces. You can place any number of name-value pairs within curly braces to define lattice string values. Except for the last line, each name-value pair must be followed by a comma (this is somewhat similar to the definition of associative arrays in Perl). For example:
[code]
Var oCar = {
"color": "red"
"doors": 4
"paidFor": true
}
1.3 mixed literals
We can mix objects and arrays literally to create an array of objects, or an object that contains an array. For example:
The copy code is as follows:
{comments: [
{
Id:1
Author: "someone1"
Url: "http://someone1.x2design.net",
Content: "hello"
}
{
Id:2
Author: "someone2"
Url: "http://someone2.x2design.net",
Content: "hello"
}
{
Id:3
Author: "someone3"
Url: "http://someone3.x2design.net",
Content: "hello"
}
]}
1.4 JSON syntax
In the Ajax application, the server generates the javascript statement directly, and the client directly uses the eval method to obtain the object, so the performance loss of parsing XML can be saved. At the same time, the benefits of using JSON as a data format in javascript communications are starry, and you can get the value of the data immediately, so you can access the data contained in it more quickly.
Var oCarInfo = eval ("(" + sJSON + ")")
Remember: curly braces are also a statement in javascript. The only way to let the parser know that the curly braces represent an object rather than a statement is to find parentheses that encapsulate it (which is used to indicate that the code is an expression rather than a statement).
1.5 JSON encoding and decoding
As part of the JSON resources, Corockford has developed a tool that enables direct decoding and encoding of JSON and Javascript objects. The source code for this tool can be downloaded from www.crockford.com/JSON/json.js.
The use of eval () mentioned above has some inherent drawbacks: it is used to evaluate any incoming Javascript code, not just JSON. Therefore, when it comes to enterprise-level web application development, it has great security risks. To solve this problem, you can use the parser JSON.parse () method, which is only used to convert JSON code into Javascript. For example:
Var oObject = JSON.parse (sJSON)
At the same time, it also provides a tool to convert Javascript objects into JSON strings (used for data transfer) (there is no built-in support for this in Javascript). All you have to do is pass the object into the JSON.Stringify () method. Take a look at the following example:
The copy code is as follows:
Var oCar = new Object ()
OCar.doors = 4
OCar.color = "blue"
OCar.year = 1995
OCar.drivers = new Array ("Penny", "Dan", "Kris")
[xss_clean] (JSON.stringify (oCar))
This code will output the JSON string as shown below:
{"doors": 4, "color": "blue", "year": 1995, "drivers": ["Penny", "Dan", "Kris"]}
2. JSON and XML
As mentioned above, one of the great advantages of JSON over XML is that it is simpler.
Take a look at the XML data representation example:
Use XML to indicate:
The copy code is as follows:
one
Someone1
Http://someone1.x2design.net
Hello
two
Someone2
Http://someone2.x2design.net
Someone1
three
Someone3
Http://someone3.x2design.net
Hello
Use JSON to indicate:
The copy code is as follows:
{comments: [
{
Id:1
Author: "someone1"
Url: "http://someone1.x2design.net",
Content: "hello"
}
{
Id:2
Author: "someone2"
Url: "http://someone2.x2design.net",
Content: "hello"
}
{
Id:3
Author: "someone3"
Url: "http://someone3.x2design.net",
Content: "hello"
}
]}
It is easy to find that a lot of redundant information is missing. Since there is no need for an ending tag (closing tag) that matches the opening tag (opening tag), the number of bytes required to transmit the same information is greatly reduced. Founder Corockford called it "XML's weight loss program").
Compared with XML, the disadvantage of data in JSON format is that it is less readable to laymen. Of course, there is a view that the data exchange format is not observed with the naked eye. If the data sent back and forth is created and parsed through a tool, there is really no reason to require that the data must be easy for people to read. The essence of the problem is that there are available JSON tools.
3. Server-side JSON tools
Java: java JSON tool, developed by Douglas Crock ford, available at www.crockford.com/JSON/java/
It can be used in JSP.
4. Advantages and disadvantages of JSON
JSON not only reduces the performance and compatibility problems caused by parsing XML parsing, but also is very easy to use for javascript. It is convenient to get data by traversing arrays and accessing object properties. Its readability is also good, and it basically has the nature of structured data. It has to be said that it is a very good way, and in fact, google maps does not use XML to transmit data, but uses the JSON scheme.
Another advantage of JSON is cross-domain feasibility, for example, it is completely feasible for you to use it in www.xxx.com pages, which means that you can send messages across domains. However, cross-domain information can not be obtained by using XMLHttpRequest, which is limited by the security nature of javascript.
JSON looks beautiful, can it completely replace XML? This is not the case, and the reason lies in the advantage of XML: versatility. It is not easy to generate syntax-qualified javascript code on the server side, which mainly occurs in relatively large systems, where there are different developers on the server side and on the client side. They must negotiate the format of the object, which can easily lead to errors.
These are all the contents of the article "how to transfer data in Ajax". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.