The processing of the Java cross-domain problem

Source: Internet
Author: User

1,javascript because of security considerations, the page is not allowed to invoke other pages of the object across the domain, then the question is, what is a cross-domain problem?

A: This is due to browser-origin policy restrictions, and now all JavaScript-enabled browsers use this policy. So what is homologous? The so-called homologous refers to three aspects of "the same":
(1) Same domain name
(2) Same agreement
(3) Same port

All three of the above are of the same origin, and JavaScript allows communication by default within the same origin range. We then recognize the same-origin and cross-domain issues through different URLs:

Http://www.a.com/a.js http://www.a.com/b.js protocol Same, domain name same, port same allows http://www.a.com/a.js  http://www.b.com/a.js  The same protocol, different domain name, the same port    does not allow the same http://www.a.com:8000/a.js http://www.a.com/b.js  protocol, the same domain name, different ports    do not allow https:// Www.a.com/a.js  http://www.a.com/b.js  protocol different, domain name is the same, port same    does not allow

Dealing with cross-domain issues in 2.Java typically takes two approaches:

(1) The background code adds header settings to the requested servlet.

Response.setheader ("Access-control-allow-origin", "*"); Response.getwriter (). Write ("{' Status ': ' OK '}");

Access-control-allow-origin This header is used in the Web Standard to check whether the cross-domain request can be passed, and if the value is * It indicates that the current page can be accessed across domains. By default, this is not allowed.

In the front-end JS code you need to make a request to the servlet, as shown in the request code:

$.ajax ({       URL: "http://localhost:8080/test",       type: "Post",       data:{            name: "Jim",            },       DataType : "JSON",       success:function (data) {...             },       error:function (data) {             ...       }     })

(2) How cross-domain requests are jsonp.

JSONP and JSON are only one letter apart, but they are completely different, and many people can easily confuse them. JSON is a form of data exchange, and Jsonp is an unofficial cross-domain data interaction protocol. This method can only be used for get-mode requests and cannot be used for post-mode requests.

Let's start by looking at how the previous JS code sends the request:

$.ajax ({           URL: "http://localhost:8080/test",           type: "Get or post",           Async:true,           dataType: "Jsonp", The           //server is used to receive the parameter of the function name of the callback call           JSONP: "Callbackparam",           //callback's function name           jsonpcallback: " Success_jsonpcallback ",           success:function (data) {                    console.log (data);           },           error:function (data) {                    console.log (data);           } });

Here the Callbackparam and Success_jsonpcallback can be understood as the data sent by the key value pair, can be customized, but callbackparam need and background to contract the parameter name, Because the background needs to get the value inside this parameter (that is, success_jsonpcallback).

The background code is as follows:

String callback = Request.getparameter ("Callbackparam"); String JSON = callback + "({' status ': ' OK '})"; Response.getwriter (). print (JSON);

In the background code, you need to get the value of the parameter named Callbackparam, and the value obtained here is "Success_jsonpcallback". Then add a pair of parentheses to the value and enclose the data you want to return, such as I return a JSON object. Of course you can also return other objects, such as returning only one string type of data. The last front-end JS received the data is this:

Success_jsonpcallback ({' Status ': ' OK '})

The browser automatically resolves to a JSON object, and you just need to use data.status directly in the success callback function.

The processing of the Java cross-domain problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.