修复跨域报错
This commit is contained in:
@@ -30,7 +30,7 @@ class AllowCrossDomain implements MiddlewareInterface
|
|||||||
'Access-Control-Allow-Credentials' => 'true',
|
'Access-Control-Allow-Credentials' => 'true',
|
||||||
'Access-Control-Max-Age' => '1800',
|
'Access-Control-Max-Age' => '1800',
|
||||||
'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
|
'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
|
||||||
'Access-Control-Allow-Headers' => 'Content-Type, Authorization, batoken, ba-user-token, think-lang, lang, server',
|
'Access-Control-Allow-Headers' => 'Content-Type, Authorization, batoken, ba-user-token, think-lang, lang',
|
||||||
];
|
];
|
||||||
$origin = $request->header('origin');
|
$origin = $request->header('origin');
|
||||||
if (is_array($origin)) {
|
if (is_array($origin)) {
|
||||||
@@ -48,6 +48,14 @@ class AllowCrossDomain implements MiddlewareInterface
|
|||||||
|| ($host === 'localhost' || $host === '127.0.0.1');
|
|| ($host === 'localhost' || $host === '127.0.0.1');
|
||||||
if ($allowed) {
|
if ($allowed) {
|
||||||
$header['Access-Control-Allow-Origin'] = $origin;
|
$header['Access-Control-Allow-Origin'] = $origin;
|
||||||
|
// 回显浏览器在预检中声明的请求头,避免白名单遗漏导致 CORS 失败
|
||||||
|
$reqHeaders = $request->header('access-control-request-headers', '');
|
||||||
|
if (is_array($reqHeaders)) {
|
||||||
|
$reqHeaders = $reqHeaders[0] ?? '';
|
||||||
|
}
|
||||||
|
if (is_string($reqHeaders) && trim($reqHeaders) !== '') {
|
||||||
|
$header['Access-Control-Allow-Headers'] = $reqHeaders;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return response('', 204, $header);
|
return response('', 204, $header);
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ function createAxios<Data = any, T = ApiPromise<Data>>(axiosConfig: AxiosRequest
|
|||||||
timeout: 1000 * 10,
|
timeout: 1000 * 10,
|
||||||
headers: {
|
headers: {
|
||||||
'think-lang': config.lang.defaultLang,
|
'think-lang': config.lang.defaultLang,
|
||||||
server: true,
|
|
||||||
},
|
},
|
||||||
responseType: 'json',
|
responseType: 'json',
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user