PHP解决跨域问题 Response to preflight request doesn‘t pass access control check: It does not have HTTP ok status.

2021年9月23日16:34:48 发表评论 450 次浏览

今天同事遇到了这样一行报错,前端请求出现

Response to preflight request doesn‘t pass access control check:It does not have HTTP ok status.

百思不得其解,于是搜了一些教程,发现都没解决我的问题,经过抽丝剥茧,一点一点解决了这个疑难问题,关键问题是前端请求时用了OPTIONS方法,虽然他一直说的是post方法,但是vue底层封装了一些东西,可能他自己也不清楚,我在他使用options方法请求的时候直接放行,获取请求的header,直接返还过去

 header('Access-Control-Allow-Origin:*');
 header("Access-Control-Allow-Headers: ".$this->request->header('Access-Control-Request-Headers'));
 header('Access-Control-Allow-Credentials: true');
 header('Access-Control-Allow-Methods: POST,GET,OPTIONS');
 if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
      header('HTTP/1.1 200 OK');
 }
阿修罗

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: