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");
            }
         });

 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    }

 

posted @ 2018-03-22 20:47  一免  阅读(134)  评论(0编辑  收藏  举报