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!

JavaandJavaScriptaredistinctlanguages:Javaisusedforenterpriseandmobileapps,whileJavaScriptisforinteractivewebpages.1)Javaiscompiled,staticallytyped,andrunsonJVM.2)JavaScriptisinterpreted,dynamicallytyped,andrunsinbrowsersorNode.js.3)JavausesOOPwithcl

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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
