Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight respon
来源:互联网 发布:宣传画册设计软件 编辑:程序博客网 时间:2024/05/19 02:04
今天在写一个项目的时候出现了一个很莫名其妙的问题,除了post请求其他的请求都可以调通,在使用post的时候,总是报以下错误。
因为在请求的header中出现以下问题:
后端是我用php撸的部署在nginx上,
错误原因:
在正式跨域的请求前,浏览器会根据需要,发起一个“PreFlight”(也就是Option请求),用来让服务端返回允许的方法(如get、post),被跨域访问的Origin(来源,或者域),还有是否需要Credentials(认证信息)
如果跨域的请求是Simple Request(简单请求 ),则不会触发“PreFlight”。Mozilla对于简单请求的要求是:
以下三项必须都成立:
1. 只能是Get、Head、Post方法
2. 除了浏览器自己在Http头上加的信息(如Connection、User-Agent),开发者只能加这几个:Accept、Accept-Language、Content-Type、。。。。
3. Content-Type只能取这几个值:
application/x-www-form-urlencoded
multipart/form-data
text/plain
解决方法如下:
第一种:
在php代码中加上Yii::$app->getResponse()->headers->set(‘Access-Control-Allow-Headers’, “content-type”);
};
public function beforeAction($action) { if(parent::beforeAction($action)) { Yii::$app->getResponse()->headers->set('Access-Control-Allow-Origin', "*"); Yii::$app->getResponse()->headers->set('Access-Control-Allow-Headers', "content-type"); }; return parent::beforeAction($action); }
第二种方法:
在nginx的配置上添加:
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
阅读全文
0 0
- Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight respon
- Request header field Content-Type is not allowed by Access-Control-Allow-Headers跨域
- Request header field Content-Type is not allowed by Access-Control-Allow-Headers跨域
- vue-resource Content-Type is not allowed by Access-Control-Allow-Headers in preflight response
- Error :Request header field Content-Type is not allowed by Access-Control-Allow-Headers post请求失败原因
- ajax跨域post提交json字符串报错Request header field Content-Type is not allowed by Access-Control-Allow-Header
- Origin null is not allowed by Access-Control-Allow-Origin.
- Origin null is not allowed by Access-Control-Allow-Origin
- XMLHttpRequest cannot load – Origin is not allowed by Access-Control-Allow-Origin.
- XMLHttpRequest Origin null is not allowed by Access-Control-Allow-Origin
- JQuery 的 ajax 出现Origin null is not allowed by Access-Control-Allow-Origin 解决方法
- XMLHttpRequest在请求失败(Origin null is not allowed by Access-Control-Allow-Origin.)
- Origin XX is not allowed by Access-Control-Allow-Origin.解决办法
- Origin null is not allowed by Access-Control-Allow-Origin解决方案
- XMLHttpRequest在请求失败(Origin null is not allowed by Access-Control-Allow-Origin.)
- XMLHttpRequest cannot load – Origin is not allowed by Access-Control-Allow-Origin.
- XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin
- jquey XMLHttpRequest cannot load url.Origin null is not allowed by Access-Control-Allow-Origin
- Tensorflow的一些常见函数
- JavaScript执行顺序导致的问题
- angular全选反选批量删除
- Intent机制详解
- 编写高性能的Lua代码
- Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight respon
- iptables规则重启自动生效--永久生效
- 手机端弹出遮罩层时禁止底层滚动
- 算法导论第三版第七章课后答案
- IMEI号和MEID号
- 关于java正则匹配返回匹配的内容
- Linux
- 多条目 加载
- Android中实现静态的默认安装和卸载应用