[Related learning recommendations: java basic tutorial]
1. What is JSONP
Generally speaking, the web page located at server1.example.com cannot communicate with a server other than server1.example.com. The exception is the HTML <script> element. Using this open policy of the <script> element, web pages can obtain JSON data dynamically generated from other sources, and this usage pattern is called JSONP. The data captured with JSONP is not JSON, but arbitrary JavaScript, which is executed with a JavaScript interpreter instead of parsed with a JSON parser. <br/></script>
JSONP is a protocol designed to solve the problem of cross-domain requests from clients to servers, but it is not a formal transmission protocol. A key point of this protocol is to allow users to pass a callback parameter to the server. Then when the server returns data, it will use this callback parameter as a function name to wrap the JSON data. In this way, the client can customize its own function to automatically process the return. The data.
2. Ajax requests other domain interfaces
My project needs to request another first background interface to request data, which is rendered on the page. When loading data through ajax as follows:
$.ajax({ url: 'http://www.xxx.cn/lalala?method=10082&page=1&pageSize=10', type: 'GET', dataType: 'json', timeout: 5000, contentType: 'application/json; charset=utf-8', success: function (result) { alter("aaaa"); } });
In this way, a cross-domain error occurs, as shown below:
No 'Access-Control-Allow-Origin' header is present on the requested resource . Origin 'null' is therefore not allowed access. The response had HTTP status code 500.
This means that cross-domain requests are not allowed, so what should I do? Just change it to jsonp. Just change the dataType field.
$.ajax({ url: 'http://www.xxx.cn/lalala?method=10082&page=1&pageSize=10', type: 'GET', dataType: 'jsonp', timeout: 5000, contentType: 'application/json; charset=utf-8', success: function (result) { alter("aaaa"); } });
Result: Uncaught SyntaxError: Unexpected token!
what the fuck! Obviously requesting data back, the result is still an error. The reason is that ajax requests the server, and the returned data format does not conform to the return format of jsonp. So what is the jsonp format?
Callback({msg:'this is json data'})
What kind of thing is this? Who defines it as weird! If you think so, it seems that you have not looked at point 1 carefully. JSON is a lightweight data exchange format, like xml. JSONP is a way of using JSON data. What is returned is not a JSON object, but a javaScript script containing a JSON object. But the picture above is a json string, so an error is reported.
3. Parameter return processing
One thing you will find is that when you request using the jsonp protocol, in addition to the parameters you fill in, there are also There is a parameter named callback, as shown in the figure:
Look at what this parameter is, because I did not specify the jsonp parameter when making the ajax request, then The system default parameter name is "callback". If the jsonpCallback parameter is not specified, jquery will generate a random function name, as shown in the figure above.
For example, if I configure it as follows:
$.ajax({ url: 'http://www.xxx.cn/lalala?method=10082&page=1&pageSize=10', type: 'GET', dataType: 'jsonp', jsonp:'callbacka',//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback) jsonpCallback:"success_jsonpCallback",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名 timeout: 5000, contentType: 'application/json; charset=utf-8', success: function (result) { alter("aaaa"); } });
Then the server can also obtain the callback function name through the following method:
The code is as follows:
string callbackFunName =request.getParameter("callbacka");//获取的就是success_jsonpCallback 字符串
Note: The system is case-sensitive in function names.
Then let’s package it according to the format:
String callback = request.getParameter("callback"); //不指定函数名默认 callback return callback+ "(" + jsonStr + ")"
After packaging it, the result is really no error. Take a look at the returned data as shown below:
4. JSONP execution process
First register a callback on the client (such as: 'jsoncallback'), Then pass the callback name (such as: jsonp1236827957501) to the server. Note: After the server gets the callback value, it must use jsonp1236827957501(...) to include the json content to be output. At this time, the json data generated by the server can be correctly received by the client.
Then, use javascript syntax to generate a function. The function name is the value jsonp1236827957501 of the passed parameter 'jsoncallback'.
Finally, use the json data directly as a parameter. Place it in the function, thus generating a js syntax document and returning it to the client. The client browser parses the script tag and executes the returned javascript document. At this time, the javascript document data is passed as a parameter to the callback function predefined by the client (such as the one encapsulated by the jquery $.ajax() method in the above example. success: function (json)).
Recommended related articles: ajax video tutorial
The above is the detailed content of Let's take a look at Java Ajax jsonp cross-domain requests. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


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

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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.