/**html文件*/
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery EasyUI</title>
<script type="text/javascript" src="../jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function() {
$("#button1").click(function() {
alert("gagag");
var url="http://192.168.1.155:8080/jml/pos/test.fbi";
$.getJSON(url + "?callback=?", function(json) {
alert(json.name);
});
});
});
</script>
</head>
<input value="button" type="button" id="button1"/>
<body></body>
</html>
/**后台文件*/
/**格式要求:
在服务端通过获得callback参数(如下:value="jsonp*****")得到jQuery端随后要回调的
然后返回类似:"jsonp*****("+要返回的json对象+")";
*/
@RequestMapping(value = "/jml/pos/test.fbi")
public void testRequest(final HttpServletRequest request, final HttpServletResponse response)
throws Exception {
final String jsonp = request.getParameter("callback");
response.setContentType("text/html; charset=utf-8");
response.setCharacterEncoding("utf-8");
response.getWriter().print(jsonp+"({\"name\":\"yoyo!\"})");
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery EasyUI</title>
<script type="text/javascript" src="../jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function() {
$("#button1").click(function() {
alert("gagag");
var url="http://192.168.1.155:8080/jml/pos/test.fbi";
$.getJSON(url + "?callback=?", function(json) {
alert(json.name);
});
});
});
</script>
</head>
<input value="button" type="button" id="button1"/>
<body></body>
</html>
/**后台文件*/
/**格式要求:
在服务端通过获得callback参数(如下:value="jsonp*****")得到jQuery端随后要回调的
然后返回类似:"jsonp*****("+要返回的json对象+")";
*/
@RequestMapping(value = "/jml/pos/test.fbi")
public void testRequest(final HttpServletRequest request, final HttpServletResponse response)
throws Exception {
final String jsonp = request.getParameter("callback");
response.setContentType("text/html; charset=utf-8");
response.setCharacterEncoding("utf-8");
response.getWriter().print(jsonp+"({\"name\":\"yoyo!\"})");
}
本文展示了一个使用 jQuery EasyUI 的 HTML 页面实例,该页面中包含一个按钮点击事件,触发时将通过 JSONP 方式从指定 URL 获取数据,并展示如何在服务端正确响应 JSONP 请求。
1万+

被折叠的 条评论
为什么被折叠?



