在工具类中写一个函数
/*
* response请求跨域公共设置
/
public static HttpServletResponse SetHttpServletResponse(
HttpServletResponse response) {
response.setHeader("Access-Control-Allow-Origin", "");// * 号代表所有域名都可以访问它,建议在项目上线时候改为固定的
response.setHeader("Access-Control-Allow-Methods",
"POST, GET, OPTIONS, DELETE");// 这是请求类型
response.setHeader("Access-Control-Max-Age", "3600"); //这是缓冲
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");//读写
return response;
}
然后只需要在跨域请求那个方法里调用这个方法就可以 如:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Util.SetHttpServletResponse(response);//调用这个方法就可以了执行跨域请求了
}