Using JSONP to troubleshoot cross-domain issues-code examples

Source: Internet
Author: User

The previous time with Jsonp solved cross-domain problem, now no, put the code thought down, in the future may also be used.

JS Code

Query announcement Data

function Recentpost () {
$.getjson (cmsurl+ "/post/recentpost.json?jsoncallback=", {Count:count,categoryid:categoryid}, function (data) {


JS code, is mainly the use of jquery Getjson method.

More descriptions, from jquery documentation.

Jquery.getjson (URL,[Data],[Callback]) Overview

The JSON data is loaded via an HTTP GET request.

In JQuery 1.2, you can load JSON data for other domains, such as "myurl?callback=?", by using a callback function in the form of JSONP. Will jQuery be replaced automatically? is the correct function name to execute the callback function. Note: The subsequent code of this line will be executed before this callback function executes.

Parameters Url,[data],[callback]String,map,function V1.0

URL: Send request address.

data: The Key/value parameter to be sent.

callback: The callback function when loading succeeds.


Load 4 new pictures of cats from the Flickr JSONP API.

HTML Code:
<div id="images"></div>
JQuery Code:
$.getJSON("", function(data){  $.each(data.items, function(i,item){    $("

Loads JSON data from Test.js and displays a name field data from the JSON data.

JQuery Code:
$.getJSON("test.js", function(json){  alert("JSON Data: " + json.users[3].name);});

Load JSON data from Test.js, append parameters, and display a name field data from the JSON data.

JQuery Code:
$.getJSON("test.js", { name: "John", time: "2pm" }, function(json){  alert("JSON Data: " + json.users[3].name);});

Java code

@RequestMapping (value = "Recentpost")
public void Recentpost (Integer CategoryID, String Jsoncallback,
Integer count, model model, httpservletresponse response) {
if (CategoryID = = null) {
CategoryID = Default_category;
list<post> list = Postservice.listrecent (CategoryID, Count);
Jsonobject json = new Jsonobject ();
Json.put ("list", list);
String str=json.tojsonstring ();
Model.addattribute ("callback", list);
String str = jsonobject.tojsonstring (list);
str = jsoncallback + "(" + str + ")";
Super.returnmessage (response, str);

Use the JSON library to send a string of JSONP format to the front end. Remember the function name jsoncallback this parameter.

Using JSONP to troubleshoot cross-domain issues-code examples

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: 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.