标签:
这个问题是关于ajax跨域访问的
首先:本地file下js
var localPath="http://192.168.100.161:8080"; $(function(){ var uuid="28293c941a3a322d9d9049a48a1045a2"; var userCategoryType=3 var c_url=localPath+"/api4/xclass/getTreeNode?uuid="+uuid+"&userCategoryType="+userCategoryType; //注意下面只需一个问号 $.getJSON(c_url, function(data){ document.write(data) }); })
然后服务器代码是
@RequestMapping(value="getTreeNode", method = RequestMethod.GET) @ResponseBody public Object getTreeNode(String uuid,Integer userCategoryType,HttpServletResponse response){ User user=userService.getUserByMd5UserId(uuid); response.addHeader("Access-Control-Allow-Origin", "*"); List<Map<String, Object>> userDocCategorys=mUserCategoryService.getUserDocCategorys(Long.parseLong(String.valueOf(user.getUserId())), userCategoryType); List<Map<String, Object>> ztreeNodes=new ArrayList<Map<String,Object>>(); for (Map<String, Object> userCategory : userDocCategorys) { Map<String, Object> ztreeNodeMap = new HashMap<String, Object>(); ztreeNodeMap = new HashMap<String, Object>(); ztreeNodeMap.put("id",userCategory.get("user_category_id") ); ztreeNodeMap.put("pId", userCategory.get("parent_id")); ztreeNodeMap.put("name", userCategory.get("user_category_name")); ztreeNodes.add(ztreeNodeMap); } return ztreeNodes; }
死活没法请求到,然后百度了下,发现是js的跨域,找了几个方法都不成功,包括jsonp等,最后
消息头中加入
response.addHeader("Access-Control-Allow-Origin", "*");
很久没记笔记了,养成好习惯,加油
标签:
原文地址:http://www.cnblogs.com/321qwe/p/4656783.html