N久N久没写代码。

今天和VUE配合调用接口的时候发现跨域报错。

中间件,入口文件、路由都折腾了好几遍。有以下几个错误来回变。

错误1:

Access to XMLHttpRequest at 'http://39.xx.xxxx.145/columns?_t=1604652551912' from origin 'http://192.168.199.243:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

错误2:

Access to XMLHttpRequest at 'http://39.104.126.145/columns?_t=1604655553900' from origin 'http://192.168.199.243:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

错误3 

Request header field timestamp is not allowed by Access-Control-Allow-Header

最后的解决方案

入口文件index.php 最上面写这样一段

THINKPHP 跨域报错解决方案

header("Access-Control-Allow-Origin: *");

if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){

header("Access-Control-Allow-Headers: *");

header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');

exit;

}

相关文章: