Mastering Ajax, part 10th: data transfer using JSON

Source: Internet
Author: User
Tags json requires

Manipulating JavaScript objects in native mode

Introduction: When sending and receiving information in an asynchronous application, you can choose to use plain text and XML as the data format. This issue of mastering Ajax discusses another useful data format, JavaScript Object Notation (JSON), and how to use it to move data and objects more easily in your application.

If you have read the previous articles in this series, you should have a good understanding of the data format. The previous article explains how to properly use plain text and simple name/value pairs in many asynchronous applications. You can combine the data into the following form:

firstName=Brett&lastName=McLaughlin&email=brett@newInstance.com

That's all you need to do. In fact, the WEB veteran will realize that the information sent through a GET request is in this format.

Then, this series discusses XML. Obviously, XML has received considerable attention (both positive and negative) and has been widely used in Ajax applications. For more information about how to use XML data formats, you can review the previous articles in this series:

<request>
  <firstName>Brett</firstName>
  <lastName>McLaughlin</lastName>
  <email>brett@newInstance.com</email>
</request>

The data here is the same as what we saw earlier, but this time it was in XML format. It's no big deal; This is just another data format that allows us to use XML instead of plain text and name/value pairs.

This article discusses another data format, JavaScript Object notation (JSON). JSON looks both familiar and unfamiliar. It provides another option, and choosing a larger range is always a good thing.

The meaning of choice

Before delving into the details of the JSON format, you should understand why you should use two articles to discuss another data format (yes, JSON is also discussed in the next article in this series), especially if you already know how to use XML and plain text name/value pairs. In fact, the reason is simple: the more choices you have to solve any problem, the more likely you are to find the best solution to the problem, which is much better than using only one solution.

Review name/value pairs and XML

This series has used a lot of space to discuss situations where name/value pairs and XML are appropriate. You should always consider using name/value pairs first. For problems in most asynchronous applications, using name/value pairs is almost always the simplest solution, and it only requires very basic JavaScript knowledge.

In fact, you don't have to consider using a different data format unless you have some kind of restriction that forces you to turn to XML. Obviously, if you want to send data to a server-side program that requires input in XML format, you want to use XML as the data format. In most cases, however, XML is a better choice for servers that need to send multiple pieces of information to the application; in other words, XML is often better suited to respond to Ajax applications than to make requests from Ajax applications.

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.