后端使用PHP,前端使用vue框架,加了跨域方法,却还是一直报跨域问题
代码:
$server = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*';
header('Vary: Origin,Access-Control-Request-Headers,Access-Control-Allow-Headers,DNT,X-Requested-With,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept,Connection,Cookie,X-XSRF-TOKEN,X-CSRF-TOKEN,Authorization,WG-TOKEN,TOKEN');
header('Access-Control-Allow-Origin: '.$server);//*代表允许任何网址请求
header('Content-Type:application/x-www-form-urlencoded;charset=UTF-8;multipart/form-data');
header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Headers:Origin,Access-Control-Request-Headers,Access-Control-Allow-Headers,DNT,X-Requested-With,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept,Connection,Cookie,X-XSRF-TOKEN,X-CSRF-TOKEN,Authorization,WG-TOKEN,TOKEN');
header('Access-Control-Allow-Method:POST,GET,OPTIONS');//允许访问的方式
header('Access-Control-Expose-Headers:TOKEN'); //自定义头部TOKEN跨域
加了上面的代码,还是报错跨域问题:
header('Access-Control-Expose-Headers:TOKEN'); //自定义头部TOKEN跨域
与前端沟通传递的 TOKEN 是否传错了。
这个值需要与前端的一致才能通过跨域的问题。