JSONP Solution Ajax Cross-Domain code example

Source: Internet
Author: User
Tags json


There are two subdomains, a.xx.com and b.xx.com, now I send an AJAX request in a.xx.com a page b.xx.com a method in a controller, want to get the return data, but return state 302, should be a cross-domain problem unresolved.


One way to solve the Ajax Cross-domain problem is to use Jsonp,jquery to encapsulate Ajax methods and Getjason methods, and cross domain issues in the work, documenting JSONP usage and examples.

(function ($) {
var url = '/scripts/jquery4u-sites.json?callback=? ';

$.ajax ({
Type: ' Get ',
Url:url,
Async:false,
Jsonpcallback: ' Jsoncallback ',
ContentType: "Application/json",
DataType: ' Jsonp ',
Success:function (JSON) {
Console.dir (json.sites);
},
Error:function (e) {
Console.log (E.message);
}
});

}) (JQuery);

This is the AJAX approach to jquery, using JSONP to define parameter references as code. Often the requested URL is followed by a callback=?, the question mark can be followed by a parameter, or you can leave blank. This example does not do a specific explanation, only record usage.

/* Loading JSON objects using JSONP */
(function ($) {
var url = '/scripts/jquery4u.settings.json ';
$.ajax ({
Type: ' Get ',
Url:url,
Async:false,
ContentType: "Application/json",
DataType: ' Jsonp '
});
}) (JQuery);

This example is a direct request to the JSON file.

$.getjson (' Ajax/test.json ', function (data) {
var items = [];

$.each (data, function (key, Val) {
Items.push (' <li id= ' + key + ' "> ' + val + ' </li> ');
});

$ (' <ul/> ', {
' Class ': ' My-new-list ',
Html:items.join (")"
}). Appendto (' body ');
});

In this case, the Getjson method request using jquery, the Getjson method automatically supports JSONP, is a jquery encapsulated good.


<script>
The client uses the Getjson method to request a script on another machine
The browser generates a random callback parameter
$.getjson ("/new/ajax_jsonp.php?callback=?", function (JSON) {
alert (json.website);
});
</script>

Getjson cross-Domain Request example, note that the URL behind the band? Callback=, otherwise is the general request JSON return data, not cross-domain, with this, plus no parameters depending on the situation, that is, Jsonp Cross-domain.

As for the parameters mentioned above, take a callback=?

Starting with version 1.2, JQuery has local support for JSONP callbacks. If you specify a JSONP callback, you can load the JSON data that is located in another domain, and the callback syntax is: url?callback=? JQuery automatically will? Replace with the name of the build function to invoke.

You can also give callback with parameters, such as? Callback=hello, then PHP (or other language programs) first get $_get[' callback ', the output is: $_get[' callback ']+ (JSON format data), Then on the page, there is a function hello () method to process the returned data. (This is just a general description of the process, generally do not add callback parameters, directly with a question mark)

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.