Jquery跨域访问java接口

时间:2023-12-21 08:54:50

Jquery前台代码:

<script>
$.getJSON("http://跨域URL/Example/text.do?jsonpCallback=?",
function(data){
      console.log(data);
}
);
</script>

后台java代码:

/**
* Example
* @author admin
*
*/
@Controller
@RequestMapping("Example")
public class ExampleController { @RequestMapping("test")
@ResponseBody
public String test(HttpServletRequest request,HttpServletResponse response){
String jsonpCallback = request.getParameter("jsonpCallback");
return jsonpCallback+"([{\"data1\":\"测试数据\",\"data2\":\"测试数据\"}])";
}

返回内容:jsonp138512212511([{"data1":"测试数据","data2":"测试数据"}])