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.
ExampleDescribe:
Load 4 new pictures of cats from the Flickr JSONP API.
HTML Code:
<div id="images"></div>
JQuery Code:
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data){ $.each(data.items, function(i,item){ $("
Describe: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);});
Describe: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