使用spring MVC Rest服务在Ajax调用中克服Same Origin Policy的最佳方法是什么?是添加标头值还是使用JSONP?
如何在Spring Mvc REST中使用RequestParam和PathVariable映射请求?
@RequestMapping(value = "/getstudent/{userId}", method = RequestMethod.GET)
public @ResponseBody Student getStudent(@PathVariable String userId, @RequestParam("callback") String callback) {
StudentRegister studentRegister = new StudentRegister();
return studentRegister.getStudent(userId);
}
答案 0 :(得分:0)
您可以检查是否允许Origin
标头。如果是,请将Access-Control-Allow-Origin
标头添加到响应中:
@RequestMapping("xhr")
@ResponseBody
public Object xhr(@RequestHeader("Origin") String origin, HttpServletResponse response) {
if (LIST_OF_AUTHORIZED_ORIGINS.contains(origin)) {
response.setHeader("Access-Control-Allow-Origin", origin);
}
return toJson();
}