html采用vue中ajax调用restful api的跨域问题解决

1.采用jsonp方式解决:

   返回的格式符合json格式,并在外围用callback括起来 返回

2.自己编写过滤器

    

package com.huahui.energy.common.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class CroFilter
 */
public class CroFilter implements Filter {

    /**
     * Default constructor. 
     */
    public CroFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		if(response instanceof HttpServletResponse){  
            HttpServletResponse alteredResponse = ((HttpServletResponse)response);  
            alteredResponse.addHeader("Access-Control-Allow-Origin", "*");  
            alteredResponse.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");  
            alteredResponse.addHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");  
            alteredResponse.addHeader("Access-Control-Max-Age", "1728000");  
        }  

		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}

你可能感兴趣的:(exception)