Home  >  Article  >  Web Front-end  >  The difference and usage between json and jsonp

The difference and usage between json and jsonp

一个新手
一个新手Original
2017-10-02 09:44:221551browse

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
}

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 3f1c4e4b6b16bbbd69b2ee476dc4f83a, a1f02c36ba31691bcfe87b2722de723b, d5ba1642137c3f32f4f4493ae923989c);
3. It can be judged that at the current stage, if you want to use the pure web side (ActiveX control, There is only one possibility for cross-domain access to data (server-side proxy, future HTML5 Websocket and other methods are not included), and that is to try to load the data into a js format file on the remote server for client calling and further processing;
 4. We happen to already know that there is a pure character data format called JSON that can describe complex data concisely. What’s even better is that JSON is also natively supported by js, so the client can process data in this format almost as desired. ;
5. This solution is ready. The web client calls the js format file dynamically generated on the cross-domain server (usually with JSON as the suffix) in exactly the same way as the calling script. It is obvious that the reason why the server To dynamically generate a JSON file, the purpose is to load the data needed by the client into it.
 6. After the client successfully calls the JSON file, it will obtain the data it needs. The rest is to process and display according to its own needs. This method of obtaining remote data looks very much like AJAX , but it’s actually not the same.
 7. In order to facilitate the client to use data, an informal transmission protocol has gradually formed. People call it JSONP. One of the key points of this protocol is to allow users to pass a callback parameter to the server, and then the server returns the data. This callback parameter will be used as a function name to wrap the JSON data, so that the client can customize its own function to automatically process the returned data.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
    // 得到航班信息查询结果后的回调函数
    var flightHandler = function(data){
        alert(&#39;你查询的航班结果是:票价 &#39; + data.price + &#39; 元,&#39; + &#39;余票 &#39; + data.tickets + &#39; 张。&#39;);
    };
    // 提供jsonp服务的url地址(不管是什么类型的地址,最终生成的返回值都是一段javascript代码)
    var url = "http://flightQuery.com/jsonp/flightResult.aspx?code=CA1998&callback=flightHandler";
    // 创建script标签,设置其属性
    var script = document.createElement(&#39;script&#39;);
    script.setAttribute(&#39;src&#39;, url);
    // 把script标签加入head,此时调用开始
    document.getElementsByTagName(&#39;head&#39;)[0].appendChild(script);
    </script>
</head>
<body>
 
</body>
</html>

The above is the detailed content of The difference and usage between json and jsonp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn