jsonp —— 跨域请求遇到的问题,服务接口返回View 对象才成功

前端之家收集整理的这篇文章主要介绍了jsonp —— 跨域请求遇到的问题,服务接口返回View 对象才成功前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
项目中用了cas 作为SSO 实现。则系统跨域请求似乎必须满足三个条件才能成功返回数据(json):

1、ajax请求数据类类型dataType 必须为jsonp

2、请求的接口必须要使用SSO 的客户端进行验证

3、请求的接口返回类型不能为Map<key,value>对象(试过不成功),改为View 对象才成功,对于能否返回String 类型没验证。


示例:

项目A 中ajax请求:

$.ajax({
url:"项目B的某个服务接口",type:"GET",dataType:"jsonp",data:{page:1,pageSize:10,queryString:null},success:function(data){
//。。。
}
});


项目B的服务接口:

@H_404_50@@RequestMapping(value="/getData",method=RequestMethod.GET) public View getBeans( @RequestParam(required=false) Long page,@RequestParam(required=false) Long pageSize,@RequestParam(required=false) String queryConfig,HttpServletRequest req,HttpServletResponse resp,Model model){ UserVo user = new UserVo(); try { //必须调用SSO 客户端验证,不管成功与否 user = AuthenticateInfoUtil.getAuthenticateUserInfo(req,resp,null); if(user == null){ model.addAttribute("mes","用户登录,不能进行此操作!"); model.addAttribute("status",SERVER_STATUS_Failed); return JSON; } }catch(Exception e){ System.out.println("异常"); } user.setUser_id(1L); user.setUser_name("system"); user.setOrganization_id(1L); user.setOrganization_name("组织名"); // 返回类型不能为Map对象,此处JSON 为MappingJackson2JsonView 实例 return JSON; } 原文链接:https://www.f2er.com/json/288717.html

猜你在找的Json相关文章