Home >Web Front-end >JS Tutorial >Ajax cross-domain (jsonp) method of calling JAVA background

Ajax cross-domain (jsonp) method of calling JAVA background

php中世界最好的语言
php中世界最好的语言Original
2018-03-31 15:42:431821browse

This time I will bring you the method of Ajax cross-domain (jsonp) calling JAVA backend. What are the precautions for Ajax cross-domain (jsonp) calling JAVA backend. The following is a practical case. Let’s take a look. one time.

1. JSONP definition

JSONP is the abbreviation of English JSON with Padding and is an unofficial protocol. 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, the 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