# jsonp **Repository Path**: xiaoym/jsonp ## Basic Information - **Project Name**: jsonp - **Description**: spring mvc框架 关于jsonp的处理 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 27 - **Forks**: 9 - **Created**: 2015-09-13 - **Last Updated**: 2024-07-18 ## Categories & Tags **Categories**: json-tools **Tags**: None ## README # Spring MVC 关于jsonp跨域处理 * 新建`JsonpAdvice`控制器增强继承`org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice`类 package com.drore.jsonp.advice; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice; @ControllerAdvice public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice{ public JsonpAdvice() { super("callback","jsonp"); } } * 所有controller类使用`@RestController`注解 package com.drore.jsonp.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HomeController { @RequestMapping(value="/render.json") public List> render(){ List> list=new ArrayList>(); for (int i = 0; i < 10; i++) { Map map=new HashMap(); map.put("userName", "张三"+i); map.put("sex", "男"); map.put("phone", "1598723212"+i); list.add(map); } return list; } } * `jQuery`跨域调用: $.ajax({ url:'http://localhost:9090/render.json', dataType:'jsonp', success:function(data){ console.log(data) } }) //返回json数据 jQuery162036356921307742596_1442105501105([ { "phone": "15987232120", "sex": "男", "userName": "张三0" }, //...... ]);