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