跨域调用的完整实例(用jquery调用)

/**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!\"})");

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值