The difference and usage between json and jsonp

一个新手
Release: 2017-10-02 09:44:22
Original
1467 people have browsed it

The difference and various usages of json and jsonp
Although there is only one letter difference between JSON (JavaScript Object Notation) and JSONP (JSON with Padding), they are actually not the same thing at all: JSON is a kind of data interchange format, and JSONP is an unofficial cross-domain data exchange protocol created by the ingenuity of developers. Let’s use the recent popular spy movie as an analogy. JSON is the “code” used by underground parties to write and exchange information, while JSONP is the connection method used to transmit information written in code to their comrades. One is to describe the format of the information, and the other is the method agreed upon by both parties for transmitting the information.
Advantages of JSON:
1. Based on plain text, cross-platform transmission is extremely simple;
2. Javascript is natively supported, and almost all backend languages are supported;
3. Lightweight data format, occupying characters The quantity is very small, especially suitable for Internet delivery;
4. It is highly readable. Although it is not as clear as XML, it is still easy to identify after reasonable indentation;
5. Easy to write and For parsing, of course, the premise is that you need to know the data structure;
Of course, there are also shortcomings of JSON, but in the author's opinion, they are really irrelevant, so they will not be explained separately.
JSON format or rules:
JSON can describe the data structure in a very simple way. It can do everything XML can do, so there is no distinction between the two in terms of cross-platform. Equally equal.
1. JSON has only two data type descriptors, braces {} and square brackets []. The remaining English colons are mapping characters, English commas are delimiters, and English double quotes "" is the definer.
 2. Curly brackets {} are used to describe a set of "different types of unordered key-value pair sets" (each key-value pair can be understood as an OOP attribute description), and square brackets [] are used to describe a set of " "Ordered data collection of the same type" (which can correspond to OOP arrays).
 3. If there are multiple sub-items in the above two sets, they should be separated by commas.
 4. The key-value pairs are separated by English colon:, and it is recommended that the key names be added with English double quotes "" to facilitate the parsing of different languages.
 5. Commonly used data types within JSON are nothing more than strings, numbers, Boolean, dates, and null. Strings must be enclosed in double quotes, and the rest are not used. The date type is quite special, so I will not go into detail here. Yes, I just suggest that if the client does not have the function of sorting by date, then just pass the date and time directly as a string, which can save a lot of trouble.

// 描述一个人 var person = { "Name": "Bob", "Age": 32, "Company": "IBM", "Engineer": true }
Copy after login

The generation process of jsonp:
1. A well-known problem, Ajax direct request for ordinary files has the problem of cross-domain unauthorized access, regardless of whether you are a static page, dynamic web page, web page Services, WCF, as long as it is a cross-domain request, are not allowed;
2. However, we also found that when calling js files on the Web page, it is not affected by whether it is cross-domain (not only that, we also found that all requests with "src "The tags of this attribute all have cross-domain capabilities, such as

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!