VML勾画流程图(三)jsonp跨域调用

VML勾画流程图(三)jsonp跨域调用

API文档:
http://api.jquery.com/jQuery.getJSON/

官方网站的测试用例,jsonp_demo1.html:
<!DOCTYPE html>
<html>
<head>
<style>img{ height: 100px; float: left; }</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div id="images">
</div>
<script>
$.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;
});
});
</script>
</body>
</html>

我本机测试的JSP用例,客户端jsonp_demo2.html:
<html>
<head>
<title>jsonp</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$.getJSON("http://localhost:8080/easyworkflow/test/jsonp_demo2_data.jsp?callback=?", //调用JSONP
function(json){
alert(json[0].name);
}
);
</script>
</head>
<body>
This is my HTML page. <br>
</body>
</html>

服务端测试页面,jsonp_demo2_data.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% String callback=request.getParameter("callback"); %>
<% out.print(callback + "([ { name:'John中文字幕'}])");%>

改写了一下jquery的格式,jsonp_demo3.html:
<html>
<head>
<title>jsonp</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$.ajax({
async:false,
url: 'http://localhost:8080/easyworkflow/test/jsonp_demo3_data.jsp', // 跨域URL
type: 'GET',
dataType: 'jsonp',
jsonp: 'jsoncallback', //默认callback
timeout: 10000,
success: function (json) {
//客户端jquery预先定义好的callback函数,成功获取跨域服务器上的json数据后,
//会动态执行这个callback函数
alert(json[0].name);
}
});
</script>
</head>
<body>
This is my HTML page. <br>
</body>
</html>
数据文件jsonp_demo3_data.jsp问价如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% String jsoncallback=request.getParameter("jsoncallback"); %>
<% out.print(jsoncallback + "([ { name:'John中文字幕'}])");%>


并将代码改为groovy的,而不是jsp的,jsonp_demo3.html如下:
<html>
<head>
<title>jsonp</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$.ajax({
async:false,
url: 'http://localhost:8080/easyworkflow/groovy.do?method=jsonpsample', // 跨域URL
type: 'GET',
dataType: 'jsonp',
jsonp: 'jsoncallback', //默认callback
timeout: 10000,
success: function (json) {
//客户端jquery预先定义好的callback函数,成功获取跨域服务器上的json数据后,
//会动态执行这个callback函数
alert(json[0].name);
}
});
</script>
</head>
<body>
This is my HTML page. <br>
</body>
</html>

groovy文件的内容GroovyController.groovy如下:
@RequestMapping(params = "method=jsonpsample")
public ModelAndView jsonpsample(HttpServletRequest request,
HttpServletResponse response) throws IOException {
ModelAndView view = new ModelAndView("jsonpView");
String jsoncallback = request.getParameter("jsoncallback");
String jsonp = jsoncallback + "([{name:'John'}])";
System.out.println(jsonp);
view.addObject("jsonp",jsonp);
return view;
}

要展示的文件,jsonpView.vm如下:
#set($layout = "layout/layout_empty.vm")
${jsonp}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值