后端
nginx
#PHP-INFO-START PHP引用配置,可以注释或修改
#设置允许跨域 location ~ [^/]\.php(/|$) { if ($request_method = ‘OPTIONS‘) { add_header Access-Control-Allow-Methods GET,POST,OPTIONS; add_header Access-Control-Allow-Origin ‘域名‘; add_header Access-Control-Allow-Credentials true; add_header Access-Control-Allow-Headers ‘Content-Type‘; return 204; } try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi-73.sock; fastcgi_index index.php; include fastcgi.conf; include pathinfo.conf; } #PHP-INFO-END
#设置转发
location ^~ /api {
proxy_pass http://localhost:9871;
}
php
header(‘Access-Control-Allow-Origin:‘.‘域名‘); header(‘Access-Control-Allow-Methods:GET, POST, OPTIONS‘); header(‘Access-Control-Allow-Headers:X-DS-KEY,Content-Type‘); header(‘Access-Control-Allow-Credentials:true‘); if($_SERVER[‘REQUEST_METHOD‘] == ‘OPTIONS‘){ return Response::create()->code(204); }
前端
axios
axios.defaults.withCredentials = true
原文:https://www.cnblogs.com/xuanjiange/p/14736258.html