Jelajahi Sumber

'解决预处理出错'

Ethan 6 tahun lalu
induk
melakukan
00867ae118
1 mengubah file dengan 13 tambahan dan 2 penghapusan
  1. 13 2
      public/index.php

+ 13 - 2
public/index.php

@@ -12,9 +12,20 @@
 // [ 应用入口文件 ]
 //编码
 header('content-type:text/html;charset=utf-8');
+// 处理预请求报错(将请求的头部数据全部允许,然后直接返回).
+if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
+    header("Access-Control-Allow-Origin: *");
+    header("Access-Control-Allow-Credentials", "true");
+    header("Access-Control-Max-Age", "3600");
+    header("Access-Control-Allow-Headers: Origin, X-Requested-With, X-Custom-Header, Content-Type, Accept, Authorization, userToken, apiToken");
+    header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
+    header('Access-Control-Expose-Headers', "userToken, apiToken");
+    exit;
+}
 //跨域
-header('Access-Control-Allow-Origin: *');
-header("Access-Control-Allow-Credentials: true");
+header('Access-Control-Allow-Origin:*');
+header('Access-Control-Allow-Methods:OPTIONS, GET, POST'); // 允许option,get,post请求
+header('Access-Control-Allow-Headers:x-requested-with'); // 允许x-requested-with请求头
 // 定义应用目录
 define('APP_PATH', __DIR__ . '/../application/');
 // 加载框架引导文件