Cors just can solve the problem.
Compared with JSONP, Cors is no doubt more advanced, convenient and reliable.
1. Jsonp can only implement get requests , while Cors supports all types of HTTP requests.
For example, if you need to obtain data from a server (http://www.a.com/user?id=123) as follows:
{"id": 123, "name": John, "Age": 17}
The data used in the JSONP mode request (Http://www.a.com/user?id=123?callback=foo) will then be as follows:
Foo ({"id": 123, "name": John, "Age": 17}); Of course, if the service side is more fully considered, the data returned may be as follows:
Try{foo ({"id": 123, "name": John, "Age": 17});} catch (e) {}
At this point we simply define an foo () function and dynamically create a script label to make its src attribute http://www.a.com/user?id=123?callback=foo:
<script type= "Text/javascript" src= "Http://www.a.com/user?id=123?callback=foo" ></script>
You can use the Foo function to invoke the returned data.
How to get data across domains through JSONP in jquery
The first approach is to set datatype as ' Jsonp ' in an AJAX function:
$.ajax ({2.dataType: ' Jsonp ', 3.url: ' http://www.a.com/user?id=123 ', 4.success:function (data) {5.//process data 6.} 7.}); The second method is implemented using Getjson, as long as the callback= parameter is added to the address:
$.getjson (' http://www.a.com/user?id=123&callback=? ', function (data) {2.//processing Data 3}); You can also simply use the Getscript method:
You can also define the Foo method outside of the function at this time
function foo (data) {
/Process data Information}
$.getscript (' Http://www.a.com/user?id=123&callback=foo ');
2, the use of cors, developers can use the common XMLHttpRequest to initiate requests and access to data, compared to JSONP has better error handling.
3, Jsonp mainly by the old browser support, they often do not support cors, and most modern browsers have supported the Cors.
PHP: Header ("Access-control-allow-origin: *");
HTML: <meta http-equiv= "Access-control-allow-origin" content= "*" >
If the cors contains 302 jumps, then the URL after the 302 jump will also contain the header request cors.
At present, IE8 and other mainstream browsers have supported the Cors Cross-domain, I believe this technology will be very practical in the future.