我想要一个 Javascript 函数将数据发送到 Spring 控制器并获得响应。但是,由于 strict-origin-when-cross-origin Referrer 策略,请求不会通过。
弹簧控制器:
@Controller
public class EventController {
@ResponseBody
@RequestMapping(value = "/event", method = RequestMethod.POST)
public String handleAjax(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response) {
response.setHeader("Access-Control-Allow-Origin", "*");
return "OK";
}
}
Javascript函数:
function getContextPath() {
return window.location.pathname.substring(0, window.location.pathname.indexOf("/",2));
}
function move(moveDir,velocity){
$.ajax({
type : "POST",
url : getContextPath() + "/event",
success: function(data){
console.log(data)
}
});
}
我知道我必须允许这些文件跨域。到目前为止,我尝试过的东西都不起作用。我尝试过的清单:
-> 添加@CrossOrigin(origins = "http://localhost:8081", maxAge = 3600)
到控制器
-> 添加response.setHeader("Access-Control-Allow-Origin", "*");
到控制器
-> 添加crossorigin="anonymous"
到 Javascript<script>
标签