个人博客:打开链接
1 什么是跨域
2 解决跨域的问题
3 跨域请求Controller实现
@Controller @RequestMapping("/item/cat") public class ItemCatController {
@Autowired private ItemCatService itemCatService; @RequestMapping(value="/list",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8") @ResponseBody public String getItemCatList(String callback) { ItemCatResult result = itemCatService.getItemCatList(); if (StringUtils.isBlank(callback)) { //需要把result转换成字符串 String json = JsonUtils.objectToJson(result); return json; } //如果字符串不为空,需要支持jsonp调用 //需要把result转换成字符串 String json = JsonUtils.objectToJson(result); return callback + "(" + json + ");"; } }
//第二种方法 @RequestMapping(value="/list") @ResponseBody public Object getItemCatList(String callback) { ItemCatResult result = itemCatService.getItemCatList(); if (StringUtils.isBlank(callback)) { //需要把result转换成字符串 return result; } //如果字符串不为空,需要支持jsonp调用 MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result); mappingJacksonValue.setJsonpFunction(callback); return mappingJacksonValue; }
4 js中处理



发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218188.html原文链接:https://javaforall.net
