Ajax and jsonp are not the same thing

Abstract: Due to the characteristics of the development mode of Sencha Touch 2, it basically determines that its native data interaction behavior can only be realized through AJAX. Of course, by calling the powerful PhoneGap plug-in and packaging, you can achieve 100% Socket communication and local database functions, or you can also achieve communication with the server and server-side push functions through HTML5 WebSocket, but these two methods are both It has its limitations. The former requires PhoneGap support, while the latter requires user devices to support WebSocket, so neither can be regarded as a native solution for ST2, only AJAX is native.

Due to the characteristics of the development mode of Sencha Touch 2, it basically determines that its native data interaction behavior can only be realized through AJAX. Of course, by calling the powerful PhoneGap plug-in and packaging, you can achieve 100% Socket communication and local database functions, or you can also achieve communication with the server and server-side push functions through HTML5 WebSocket, but these two methods are both It has its limitations. The former requires PhoneGap support, while the latter requires user devices to support WebSocket, so neither can be regarded as a native solution for ST2, only AJAX is native.

When it comes to AJAX, it will inevitably face two problems. The first is what format does AJAX exchange data in? The second is how to solve cross-domain requirements? These two problems currently have different solutions. For example, data can be described with custom strings or XML, and cross-domain can be solved through server-side proxies. But so far the most respected or preferred solution is to use JSON to transmit data, and to rely on JSONP to cross domains. And that's what this article is about.

Although JSON and JSONP differ by only one letter, they are not the same thing at all: JSON is a data exchange format, while JSONP is an unofficial cross-domain data interaction created by the ingenuity of developers. protocol. Let's take the recent popular spy war movies as an example. JSON is the "crypto" used by underground parties to write and exchange information, while JSONP is the connection method used to transmit the information written in the code to one's comrades. did you see? One is the format of the description information, and the other is the method agreed upon by both parties of the information transmission.

Now that we're talking casually, let's stop talking dogmatically and focus on helping developers understand if and how they should choose to use it. Just a little advertisement, this article was written in my own group when I was discussing the ST2 data interaction model with the developers of Sencha Touch 2, so if you are interested in Mobile Web App development, welcome Join the Sencha Touch communication QQ group 213119459.

What is JSON?

As mentioned earlier, JSON is a text-based data exchange method, or a data description format. Whether you should choose it should first pay attention to its advantages.

Advantages of JSON:
1. Based on plain text, cross-platform transmission is extremely simple;
2. JavaScript native support, almost all background languages ​​are supported;
3. Lightweight data format, occupying a very small number of characters, especially suitable for Internet transmission;
4. Can The readability is strong, although it is not as clear as XML, but it is still easy to identify after reasonable indentation;
5. It is easy to write and parse, of course, the premise is that you must know the data structure;
Of course, there are disadvantages of JSON, but In the author's opinion, it is really irrelevant, so I will not explain it separately.

JSON format or rules:
JSON can describe data structures in a very simple way, and XML can do it, so in terms of cross-platform, the two are completely comparable.
1. JSON has only two data type descriptors, curly brackets {} and square brackets [], the rest of the English colons: are mapping characters, English commas are delimiters, and English double quotation marks "" are delimiters.
2. Braces {} are used to describe a set of "unordered key-value pairs of different types" (each key-value pair can be understood as an OOP attribute description), and square brackets [] are used to describe a group of "the same type of Ordered data sets" (arrays that can correspond to OOP).
3. If there are multiple sub-items in the above two sets, they are separated by commas.
4. The key-value pair is separated by an English colon:, and it is recommended to add English double quotation marks "" to the key name to facilitate parsing in different languages.
5. The common data types in JSON are nothing more than strings, numbers, booleans, dates, and nulls. Strings must be enclosed in double quotes, and the rest are not needed. It is recommended that if the client does not have a functional requirement for sorting by date, it is good to pass the date and time directly as a string, which can save a lot of trouble.

JSON example:
copy code
// describe a person

var person = {

"Name": "Bob", "Age": 32, "Company": "IBM", "Engineer": true
click and drag to move
}

// Get this person's information

var personAge = person.Age;







http://click.aliyun.com/m/23944/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326616965&siteId=291194637