Js,java,ajax for cross-domain access and its rationale

Source: Internet
Author: User

http://blog.csdn.net/saytime/article/details/51540876

This article on cross-domain access to do a more detailed analysis, I do a brief summary here

1. Implement cross-domain access principle:

Browsers cannot do cross-domain access because of the same-origin policy, but the <script> tag is an open policy that can be accessed across domains, so the breach is through <script> for cross-domain access to JSON data elsewhere

Jsonp mode:

JSONP (JSON with Padding) is a "usage pattern" of JSON that can be used to address cross-domain data access issues in mainstream browsers. Because of the same-origin policy, Web pages that are generally located in server1.example.com cannot communicate with servers that are not server1.example.com, and HTML script elements are an exception. Using <script> This open strategy of the element, the Web page can get JSON data that is generated dynamically from other sources, and this usage pattern is called JSONP. The data captured with JSONP is not JSON, but arbitrary JavaScript, which executes with a JavaScript interpreter instead of parsing with a JSON parser.

2. The point to be aware of:

Get content through script only the script tag can recognize the data will not error, so want to get the data in this way, need to encapsulate this data, to be able to identify the script, we can set a callback function callback, the domain has, the other domain also has, Achieve the effect of getting the data over.

3.jquey Implementation method

$.ajax ({URL:'http://localhost:8888/other/other.jsp', type:'Post', data:{'params':'Fromjsonp'}, DataType:"Jsonp", Jsonp:"Callback",//The parameter name that is passed to the request handler or page to obtain the name of the JSONP callback function (generally by default: callback)Jsonpcallback:"GetResult",//Custom JSONP callback function name, default to jquery automatically generated random function name, or do not write this parameter, jquery will automatically handle the data for yousuccess:function (data) {}, Error:function () {alert ('fail'); }        });

Here JSONP: Through it can get the corresponding callback function getparms (callback) get the real callback function, general Jquey not specified will automatically generate a callback function

Other domains may have this function using the resulting content must conform to the script syntax, can be recognized

  

Js,java,ajax for cross-domain access and its rationale

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.