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配置一哈:


<filter>
    <filter-name>AjaxFilter</filter-name>
    <filter-class>com.soccer.filter.AjaxFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>AjaxFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

此时完美解决!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值