美文网首页
Axios+ThinkPHP遭遇跨域问题的解决

Axios+ThinkPHP遭遇跨域问题的解决

作者: 似水星尘 | 来源:发表于2018-10-12 10:42 被阅读0次

    开发的项目使用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;

        }

    相关文章

      网友评论

          本文标题:Axios+ThinkPHP遭遇跨域问题的解决

          本文链接:https://www.haomeiwen.com/subject/sosyyftx.html