


What is the difference between json and jsonp? Comparison of the differences between json and jsonp
json and jsonp are easy to confuse when you first learn, so this article will introduce the difference between json and jsonp. Friends in need can refer to it.
Without further ado, let’s get straight to the point~
The difference between json and jsonp:
First of all, we should know that JSON is a kind of data exchange format, and JSONP is an unofficial cross-domain data exchange protocol created by developers. (Students who are unclear can read these two articles: What is jsonp? Detailed explanation of the principles of jsonp. What do and json mean? What is it used for? )
Let’s take a look at the comparison between json and jsonp
json is a text-based data exchange format used to describe complex data. For example, describing a student’s information can Write like this:
var student = { "id":"001", "name":"张三", "sex":"男", "age":20 } console.log(student.id); //001 console.log(student.name); //张三
Then you can get the student's student ID and name through student.id and student.name.
json data format is commonly used in the field of front-end development. Its advantages are:
(1) Based on plain text, cross-platform transmission is simple;
(2) JavaScript is natively supported, and almost all backend languages are supported;
(3) Lightweight data format, occupying very few characters, especially suitable for Internet transmission;
(4) Strong readability ;
(5) Easy to write and parse;
jsonp is a cross-domain interaction protocol, which can be understood as jsonp stipulates how to transmit json data. To put it simply, json does not support cross-domain, but js can cross-domain. Therefore, the json data is encapsulated on the server side with the js function name provided by the client, and then the function is provided to the client to call, thereby obtaining the json data. For example:
The client code is as follows:
$(function () { var user = { "username": "HelloWorld" }; $.ajax({ url: "http://localhost:8080/Changyou/UserInfo", type: "POST", contentType: "application/json; charset=utf-8", dataType: "jsonp", //json不支持跨域请求,只能使用jsonp data: { user: JSON.stringify(user) }, jsonp: "callback", //传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名,默认为callback jsonpCallback: "userHandler", //自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据 success: function (data) { $("#user_name")[0].innerHTML = data.user_name; $("#user_teleNum")[0].innerHTML = data.user_teleNum; $("#user_ID")[0].innerHTML = data.user_ID; }, error: function () { alert("请求超时错误!"); } }) });
The server code is as follows:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json; charset=utf-8"); String username = new String(request.getParameter("user").getBytes("ISO-8859-1"),"utf-8"); String callback = new String(request.getParameter("callback").getBytes("ISO-8859-1"),"utf-8"); System.out.println("接收到的数据:" + username); System.out.println("callback的值:" + callback); JSONObject user = JSONObject.fromObject(username); System.out.println("接收到的用户名:" + user.get("username")); JSONObject userinfo = new JSONObject(); userinfo.put("user_name", "张鸣晓"); userinfo.put("user_teleNum", "18810011111"); userinfo.put("user_ID", "123456789098765432"); PrintWriter out = response.getWriter(); String backInfo = callback + "(" + userinfo.toString() + ")"; //将json数据封装在callback函数中提供给客户端 out.print(backInfo); out.close(); }
Explanation: Although the client does not implement the userHandler function, it can still run successfully. The reason is When jquery processes jsonp type ajax, it automatically generates a callback function for you and takes out the data for the success attribute method to call.
This article ends here. For more exciting content, you can pay attention to the PHP Chinese website! ! !
The above is the detailed content of What is the difference between json and jsonp? Comparison of the differences between json and jsonp. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
