Java cross-domain and implementation principles

Source: Internet
Author: User
Tags script tag

recently studied the cross-domain, no contact before I was the impression is a single sign-on one way, later on the Internet to see the information just know that not only this one, use a lot, specific can go online search.

  A well-known problem, Ajax directly request ordinary files There is cross-domain access to the problem, no matter you are static pages, dynamic Web pages, Web services, WCF, as long as the cross-domain request, is not allowed. I'm going to use port 8081 to request 8081 of my local interface to see what will be prompted?

As shown, it has been blocked by the browser, so now we need to change the Jsonp method to get the data returned by the server.

The interface of the server is as follows:

@RequestMapping (value ="/hello", method =requestmethod.get) @ResponseBody public String Hello (httpservletrequest request) {Map<string, string> map = new hashmap<string, string>(); Map.put ("result","content"); Jsonobject Resultjson= Jsonobject.fromobject (map); //Assemble JSON String jsonpcallback as needed= Request.getparameter ("Jsonpcallback"); //returns JSONP format Data System.out.println (Jsonpcallback+"("+ resultjson.tostring (1,1) +")"); returnJsonpcallback +"("+ resultjson.tostring (1,1) +")"; }

The format for stitching is: jquery164006948568608122441_1476774974068 ({"Result": "Content"}) is actually wrapped up outside of JSON, So that the data in the JSON is normally available in Ajax.

Here are the JSON and JSONP data to compare:

{    "message": "Get Succeeded",    "state": "1",    "result": {"name": "Workgroup 1", "id": 1, " Description ":" One "}}
callback ({    "message": "Get Succeeded",    "state": "1",    "result": {"name": "Workgroup 1", "id" : 1, "description": "One"})

Despite the cross-domain functionality, I've been wondering why JSONP can get the data, and with curiosity I started looking at the data online and doing my own hands-on practice and understanding, finally understanding the internal workings of the principle. The first can be found in the introduction of external CSS or JS is not subject to browser interception, such as SRC is not affected by the cross-domain, then JSONP is the use of this principle? The answer is yes, JSONP is to use the dynamically generated JS format file to load the data into the client for parsing.

Below is the core part of the SONP client implementation , through the JS assembly <script type= "TEXT/JAVASCRTPT" src= "http://xxx.xxx.com?" Callback=xxx "> JS fragment, through the SRC is not affected by the cross-domain principle of the need to access the other domain name address set to SRC attribute request.

// provide the URL address of the JSONP service (regardless of the type of address, the resulting return value is a piece of JavaScript code)            var url = "Http://localhost:8080/hello?callback=flightHandler";             // Create a script tag, set its properties            var script = document.createelement (' script ');            Script.setattribute (' src ', url);             // Add the script tag to head, and the call starts            document.getElementsByTagName (' head ') [0].appendchild (script);  

Java cross-domain and implementation principles

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.