X-Requested-With

最近工作中发现,使用angular $http跨域的时候,虽然后台已经配置了跨域允许,但是还是报错。

查资料发现,angular $http 的request的请求头中,默认有:

Access-Control-Request-Headers:x-requested-with  
 
而jQuery/fetch 等其他的ajax请求没有这个默认设置,解决办法有2种。
 
1.在client 端,angular里面设置,例如:
const app = angular.module(config.name)
    .config(function($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
    })

2.或者在server端设置跨域的时候:

res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
原文地址:https://www.cnblogs.com/rengised/p/6519597.html