ajax的跨域方案

前端之家收集整理的这篇文章主要介绍了ajax的跨域方案前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

服务端需要做改造.

1. 添加header

见下面代码.

@Override
public void doFilter(ServletRequest servletRequest,ServletResponse servletResponse,FilterChain filterChain)
		throws IOException,ServletException {
	HttpServletRequest request= (HttpServletRequest)servletRequest;
	System.out.println("a visit from :"+request.getRemoteAddr());
	HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
	httpServletResponse.setHeader("Access-Control-Allow-Origin","*");
	httpServletResponse.setHeader("Access-Control-Allow-Headers","X-Requested-With,Content-Type");
	httpServletResponse.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");  
	filterChain.doFilter(servletRequest,servletResponse);
}


2. 使用jsonP

原文链接:https://www.f2er.com/ajax/161391.html

猜你在找的Ajax相关文章