ajax jsonp 跨域请求
$.ajax({
type: 'GET',
async: false, // 设置为同步
url: "http://127.0.0.1:7080/api/testJsonP.action",
data: {
subcompanyName: "北京",
departmentname: "北京",
userName: "程少话",
requestid: "123456",
reload: false
},
dataType: "jsonp",
jsonp: "callback",
jsonpCallback: "callbackSuccess",
success: function (data) {
console.log(data, " ..................... ");
},
error: function(){
alert(">>>>>>>>> fail");
}
});
![](https://images2018.cnblogs.com/blog/989581/201803/989581-20180322205543193-1054307837.png)
1 @RequestMapping(value = "api/testJsonP", method = RequestMethod.GET)
2 @ResponseBody
3 private String testAjaxJsonP(@RequestParam(value = "callback", required = true) String callback,
4 ApiParam param, HttpServletRequest request, @RequestParam(value = "reload") boolean reload)
5 {
6 logger.info(reload + " " + param.toString());
7 JSONObject jsonObj = new JSONObject();
8 jsonObj.put("ip", getIpAddr(request));
9 jsonObj.put("time",
10 CommonUtil.DEFAULT_DATETIME_FORMAT.format(Calendar.getInstance().getTime()));
11 jsonObj.put("callback", callback);
12
13 StringBuilder json = new StringBuilder(callback);
14 json.append("(").append(jsonObj.toString()).append(")");
15 return json.toString();
16 }