jquery.min.js:4 Cross-Origin Read Blocking (CORB) blocked cross-origin
跨域:浏览器对于javascript的同源策略的限制,例如a.cn下面的js不能调用b.cn中的js,对象或数据(因为a.cn和b.cn是不同域),所以跨域就出现了.
参考:https://blog.csdn.net/xuhonglei_2004/article/details/50137137,https://blog.csdn.net/lcore/article/details/41022567,https://blog.csdn.net/u014727260/article/details/72793459
http://www.cnblogs.com/chopper/archive/2012/03/24/2403945.html 这个原理解释的清楚
页面端写为: Js代码
var params = "18"; $.ajax({ type: "GET", //提交方式 url: "http://192.168.33.110:8789/region/getDataByIdcallback/" + params,//路径 cache :false, // jsonp: "callback", jsonpCallback:"callback", //callback要与后台对应 dataType: 'JSONP', // 处理Ajax跨域问题 success: function (result) {//返回数据根据结果进行相应的处理 console.log('result', result); } });
后端写为:
@GetMapping("/getDataByIdcallback/{id}") public String getDataByIdcallback(@PathVariable("id") String id) { ResponseResult responseResult = new ResponseResult(); Map<String, Object> message = new HashMap<>(); String result = ""; try { Long dataId = Long.parseLong(id); Region region = regionRepository.findById(dataId).get(); message.put("id", region.getId()); message.put("paraLayers", region.getParaLayers()); message.put("paraBbox", region.getParaBbox()); message.put("paraCenter", region.getParaCenter()); ObjectMapper mapper = new ObjectMapper(); //也可以用传参String callbackFunName =req.getParameter("callbackparam");//得到js函数名称 //这个拼接是重点。。。callback与前端对应 result = "callback("+mapper.writeValueAsString(message)+")"; } catch (Exception e) { Map<String, Object> reError = new HashMap<>(); reError.put("status", "fail"); reError.put("data", e.getMessage()); result = reError.toString(); } return result; }