后端代码:
httpResponse.setHeader("Access-Control-Allow-Origin", httpServletRequest.getHeader("Origin"));
httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
前端代码:
$.ajax({
url: "http://localhost:8080/text",
type: "POST",
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (res) {
render(res);
}
});
注意:
跨域请求想要带上cookies的话,必须在请求头里面加上{crossDomain: true, xhrFields: {withCredentials: true}}设置。