开发的项目使用Axios,再往后台(ThinkPHP5)数据交互的时候
OPTIONS http://shapi.test.com/test/pote 404 (Not Found)
Access to XMLHttpRequest at 'http://shapi.test.com/test/pote' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
查看Nginx日志发现
127.0.0.1 - - [20/May/2019:10:55:44 +0800] "POST /test/pote HTTP/1.1" 200 2922 "-" "PostmanRuntime/7.13.0"
127.0.0.1 - - [20/May/2019:10:58:30 +0800] "OPTIONS /test/pote HTTP/1.1" 404 14385 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"
第一条是使用POSTMAN进行测试,一切正常
第二条是自己写了一个测试页面用Axios模拟POST请求,报错,相应的请求是OPTIONS
相关文章都是将非简单请求(not-so-simple request)变为 简单请求(simple request)
我换了另外一个方式,在Thinkphp\public\index.php中,增加对OPTIONS请求的单独处理
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
exit;
}
网友评论