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 <script>, <img alt="The difference and usage between json and jsonp" >, <iframe>); <br/> 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; <br/> 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. ;<br/> 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. <br/> 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. <br/> 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. <br/></script>
<!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('你查询的航班结果是:票价 ' + data.price + ' 元,' + '余票 ' + data.tickets + ' 张。'); }; // 提供jsonp服务的url地址(不管是什么类型的地址,最终生成的返回值都是一段javascript代码) var url = "http://flightQuery.com/jsonp/flightResult.aspx?code=CA1998&callback=flightHandler"; // 创建script标签,设置其属性 var script = document.createElement('script'); script.setAttribute('src', url); // 把script标签加入head,此时调用开始 document.getElementsByTagName('head')[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!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),