关于ajax跨域的问题
1.只需要在后台加上
response.setHeader("Access-Control-Allow-Credentials","true");
response.setHeader("Access-Control-Allow-Origin",request.getHeader("origin"));
2.前台ajax代码,红色为主要部分即可
$.ajax({
type : "POST",
async : false,
url: url,
dataType : "json",//数据类型为json
xhrFields: {
withCredentials: true
},
crossDomain: true,
success : function(data) {
console.log(data)
},
error : function() {
alert('fail');
}
});
完成!