This article mainly introduces the detailed explanation of Ajax cross-domain (jsonp) calling JAVA backend. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor to take a look
1. JSONP definition
JSONP is the abbreviation of English JSON with Padding, which is a Unofficial agreement. It allows script tags to be generated on the server side and returned to the client to achieve site access in the form of javascript callback. JSONP is a script tag injection that adds the response returned by the server to the page to implement specific functions.
2. The origin of JSONP
To explain the origin of JSONP, first let’s talk about the browser’s “SOP: Same Origin Policy”. In short, the browser restricts script programs to only interact with scripts of the same protocol, same domain name, and same port. This includes sharing and passing variables, etc. The delivery of cookies also follows the same policy. This causes some troubles when integrating applications involving multiple servers. The problem of cross-domain access causes the Ajax code of site A to be unable to access the data of site B.
How to solve cross-domain access? Then you need to take advantage of a feature of the browser: although the browser does not allow scripts in the page to read data across domains, it allows HTML to reference cross-domain resources, such as images, CSS, and scripts. The reference to the script program is special. After it is parsed by the browser, it is the same as the local script program and can be interpreted and executed immediately. For example, in a js file on site B, a simple prompt box: alert ("This is Victor!");. If you reference this js on site A, this script will be executed in the application on site B and display an alert message. Since the reference of off-site scripts is implemented through script tags, and the script program can control all tags of the HTML page through DOM (including dynamically creating script tags), this can be achieved by calling off-site programs. Changes were made to local resources. In addition, through the use of