Error :Request header field Content-Type is not allowed by Access-Control-Allow-Headers post请求失败原因


Error :Request header field Content-Type is not allowed by Access-Control-Allow-Headers 

后台框架:Spring Mvc

JS框架:angular Js/JQuery

解决的问题:1-头部请求失败;2-跨域

我是用angular js 进行post请求会遇到这个问题,起初我也是困惑很久,虽然页也知道是头部的问题。


最后只需要进行配置一下就可以了

package com.soccer.filter;

import com.alibaba.druid.filter.FilterChain;

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

/**
 * Created by xx on 2017/3/17.
 */
public class AjaxFilter implements javax.servlet.Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, javax.servlet.FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse httpServletResponse=(HttpServletResponse)servletResponse;
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Authentication,Origin, X-Requested-With, Content-Type, Accept");
        filterChain.doFilter(servletRequest,httpServletResponse);
    }


    @Override
    public void destroy() {

    }

}


然后在web.xml配置一哈:



    AjaxFilter
    com.soccer.filter.AjaxFilter



    AjaxFilter
    /*


此时完美解决!!!

你可能感兴趣的:(spring,mvc)