jQuery发送ajax请求实现跨域访问

 Java代码的话,在返回响应之前调用如下代码中的allowCrossDomainAccess()方法:

 

/**
 * 允许跨域访问
 */
public void allowCrossDomainAccess(){
    HttpServletResponse servletResponse = getResponse();
    servletResponse.setHeader("Access-Control-Allow-Origin", "*");
    servletResponse.setHeader("Access-Control-Allow-Methods", "POST,GET");
    servletResponse.setHeader("Access-Control-Allow-Headers:x-requested-with", "content-type");
}

public HttpServletResponse getResponse(){
    RequestAttributes ra = RequestContextHolder.getRequestAttributes();
    return ((ServletRequestAttributes) ra).getResponse();
}

 

 

参考:

https://blog.csdn.net/ldx891113/article/details/50887776

https://blog.csdn.net/fdipzone/article/details/46390573

https://blog.csdn.net/moxiaoya1314/article/details/80430300

 

全文完

:)

转载于:https://www.cnblogs.com/poterliu/p/9260173.html

你可能感兴趣的:(jQuery发送ajax请求实现跨域访问)