Ajax is related to the JSON data storage format of user interaction _ajax

Source: Internet
Author: User
Tags arrays

Data storage is the core function of JavaScript, which is an easy to confuse problem in the early learning. It's not the kind of special effects that look like page slides, slide shows, fading, and so on. Properly storing the data helps us organize the structure and make it easier for the application to access the content later. JavaScript provides a variety of different ways to store data, and it is an evolutionary paradigm that moves from basic to future:

XML represents Extensible Markup language, is a flexible data format, many applications store data like it, structure like HTML, also contains elements, tags and attributes, the model is the same. One of the big advantages of it is that it's an extensible format, that you're not constrained by a preset data structure, and that it conforms to the DOM standard, and that the data obtained using AJAX calls is like parsing HTML, which is parsed by the corresponding Dom method and attributes:

<ingredient>
<prison>
<name>apple</name>
<color>red</color>
</prison>
<prison>
<name>tomato</name>
<color>red</color>
</prison>
<prison>
<name>peach</name>
<color>pink</color >
</prison>
<prison>
<name>pitaya</name>
<color>green< /color>
</prison>
</ingredient>

A serious flaw in XML, however, is that it cannot be used for Aiax calls across domain names, meaning that each AJAX-invoked XML data must be in the same domain name as the script executing the call, or the request will fail.

HTML storage is similar, and it is the simplest combination of Ajax. The important factor in this way of data format is speed, where HTML code snippets can be exported directly to the DOM without parsing the client, so we don't need to write JavaScript code to parse HTML.

<ul>
<li><a href= "boke.js" >apple:red</a></li>
<li>tomato:red</li >
<li>peach:pink</li>
<li>piyata:green</li>
</ul>

To talk about its drawbacks, it stores data that may be mixed with HTML code, so maintainability is reduced. In addition, when writing processing code, developers may need to do some fine control on the server side.

Here are the ways to use JavaScript for data storage, step-by-step ...

Variable

JavaScript is the most basic way of data storage, it accepts strings, values, Boolean, and so on. The core of the content is its scope problem.

For variable-related performance issues:

After the variables are declared, they are cached into JavaScript files, so they can be used repeatedly in the scope, the cost of getting variables is negligible, so long as the data is used more than 1, it should be stored in the variable.

Array

arrays, with normal arrays, associative arrays, and multidimensional arrays, are lists of elements that are very flexible in a variety of data stores. Master the most basic form of the array, but also the use of other complex forms of spleen benefits. The intersection of array elements is a common basic interaction code. Given the importance of this type of data storage for arrays, JavaScript provides a number of exclusive methods for array interaction:

The. Join () method can specify that a character can be used as a connector for each element within an array; the slice () method can output a range of elements in an array, and the shift () and the Unshift () methods are applied to remove or add elements to the head of an array, Pop () method Used to remove and return the last element in the array; the concat () method is used to stitch multiple arrays into an array, depending on the sequence of arguments; the sort () method arranges the elements of an array in alphabetical order or in a custom order, but is sorted only by the first character, and is generally wrong.

Object

As the data becomes more complex and unmanageable, I choose to convert the array to an object. Because objects are stored in a more readable way.

Object data store
var meatball = {
Bread: "Wang",
meat: "Li",
cheese: "Zhang"
};
Returns the bread 
alert (meatball.bread) of the object;

The effect of object on efficiency

The above code has only one layer, but it can be nested infinitely. Object can better organize code, improve the degree of modularity. At the same time, however, the trade-off between performance and maintainability needs to be considered, because it needs to look for one of those three. If the application of object-oriented programming technology, nesting to reach 3 levels and above, will cause performance problems, but in the server-side application is not the case, so the front-end developers to decide when to use object-oriented programming technology.

Json

JSON is a data format that is easy to integrate with Jacascript, and is typically used when calling external servers with JavaScript. JSON data is stored in separate files and is typically located on another completely different server. It is now the most common type of data format used to invoke the API service, because it is easy to read because the format is selected. Initially, the JSON format emerged as an alternative to an XML format, but soon it became a mainstream in the field of data exchange. It is a lightweight data format that is easy to access across domains via remote Ajax calls. Although JavaScript provides native support for parsing JSON format, it is a platform-independent format that clients and servers can use when exchanging data.

Because the JSON format has the ability to access across domains, to prevent all malicious behavior, we must only use JSON from a trusted data source.

This is a Salad.json data file:

Ajax JSON Salad
var ingredient = {"
fruit": [
{
"name": "Apple",
"color": "Green"
},
{ c8/> "name": "Tomato",
"color": "Red"
},
{
"name": "Peach",
"color": "Pink"
},
{ c16/> "name": "Pitaya",
"color": "White"
},
{
"name": "Lettuce",
"color": "Green"
}< c23/>]
};

The most common use of JSON is in conjunction with API calls, in the context of an API-based network program development environment, using which backend language to develop, and no data format changes.

The above is a small set to introduce Ajax and user interaction JSON data storage format, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.