Method instance method of Ajax Jsonp cross-domain

Source: Internet
Author: User
Tags error handling getscript instance method

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.

Related Article

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.