使用JSONP解决跨域问题-代码示例
2015-04-13 16:40
621 查看
前段时间用JSONP解决了跨域问题,现在不用了,把代码思路记下来,今后说不定还用得上。
JS代码
//查询公告数据
function recentpost(){
$.getJSON(cmsUrl+"/post/recentpost.json?jsoncallback=?",{count:count,categoryid:categoryid}, function(data){
//
});
}
JS代码,主要就是使用jquery的getJSON方法。
更多描述,来自jquery 文档。
在 jQuery 1.2 中,您可以通过使用JSONP形式的回调函数来加载其他网域的JSON数据,如 "myurl?callback=?"。jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。 注意:此行以后的代码将在这个回调函数执行前执行。
data:待发送 Key/value 参数。
callback:载入成功时回调函数。
HTML 代码:
jQuery 代码:
jQuery 代码:
jQuery 代码:
Java代码
@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);
}
使用JSON库,把jsonp格式的字符串发送到前端。记得函数名称jsoncallback这个参数。
JS代码
//查询公告数据
function recentpost(){
$.getJSON(cmsUrl+"/post/recentpost.json?jsoncallback=?",{count:count,categoryid:categoryid}, function(data){
//
});
}
JS代码,主要就是使用jquery的getJSON方法。
更多描述,来自jquery 文档。
jQuery.getJSON(url, [data], [callback])
概述
通过 HTTP GET 请求载入 JSON 数据。在 jQuery 1.2 中,您可以通过使用JSONP形式的回调函数来加载其他网域的JSON数据,如 "myurl?callback=?"。jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。 注意:此行以后的代码将在这个回调函数执行前执行。
参数
url,[data],[callback]String,Map,FunctionV1.0
url:发送请求地址。data:待发送 Key/value 参数。
callback:载入成功时回调函数。
示例
描述:
从 Flickr JSONP API 载入 4 张最新的关于猫的图片。HTML 代码:
<div id="images"></div>
jQuery 代码:
$.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){ $("<img/>").attr("src", item.media.m).appendTo("#images"); if ( i == 3 ) return false; }); });
描述:
从 test.js 载入 JSON 数据并显示 JSON 数据中一个 name 字段数据。jQuery 代码:
$.getJSON("test.js", function(json){ alert("JSON Data: " + json.users[3].name); });
描述:
从 test.js 载入 JSON 数据,附加参数,显示 JSON 数据中一个 name 字段数据。jQuery 代码:
$.getJSON("test.js", { name: "John", time: "2pm" }, function(json){ alert("JSON Data: " + json.users[3].name); });
Java代码
@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);
}
使用JSON库,把jsonp格式的字符串发送到前端。记得函数名称jsoncallback这个参数。
相关文章推荐
- 使用JSONP解决跨域问题-代码示例
- 使用JSONP解决跨域问题-代码示例
- 使用JSONP解决跨域问题-代码示例
- 使用JSONP解决跨域问题-代码示例
- 使用JSONP解决跨域问题-代码示例
- 使用JSONP解决跨域问题的实例
- ajax使用jsonp解决跨域问题
- jquery使用jsonp解决json跨域访问的问题
- Struts2使用jQuery JSONP解决跨域问题
- 使用jsonp完美解决跨域问题
- 使用fetch轻松解决JS跨域请求问题——无需CORS,jsonp
- Java使用递归法解决汉诺塔问题的代码示例
- 【TaoTao】使用JQueryJsonp 完美解决JS跨域问题
- 前端使用ajax发送jsonp格式解决跨域访问的问题
- 使用jsonp完美解决跨域问题
- 使用jsonp解决跨域问题
- 使用jsonp解决web远程调用跨域问题
- 使用Jsonp解决跨域数据访问问题
- 使用Jsonp解决跨域数据访问问题
- 使用Jsonp解决跨域数据访问问题[转]