跨域处理

服务器端:

1
2
3
4
5
6
7
8
9
@RequestMapping("test/jsonp")
@ResponseBody
public JSONPObject jsonp(HttpServletResponse response, String userId, String callback){
response.setHeader("Access-Control-Allow-Origin", "*");
Map<String,Object> m= Maps.newHashMap();//import com.google.common.collect.Maps;
m.put("userId",userId);
JSONPObject jsonpObject=new JSONPObject(callback,m);//import com.fasterxml.jackson.databind.util.JSONPObject
return jsonpObject;
}

前端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$(function () {
$("#btn_jsonp").on('click',function () {
$.ajax({
url:'http://192.168.1.61:6666/test/jsonp',
type:'get',
dataType:'jsonp',
data:{userId:'123'},
jsonp:'callback',
jsonpCallback:'success_jsonpCallback',
success:function (data) {
alert(data.userId);
}
})
});
});