webman跨域需要注意的几个点
webman跨域需要注意的几个点
1、一般我们安装webman/cors
composer require webman/cors
安装后就会自动启用了,但是很多时候还是会跨域
2、我们在尝试用postman请求路径,但是把请求类型改为options
这时候的结果很多时候,不是预想的空结果,而是默认的404结果
3、这个时候我们需要检查下路由文件
文件位置在config/route.php
一般来说,为了安全起见,我们会关闭非路由的地址,通过以下代码
Route::disableDefaultRoute();
也会把未匹配到的路由,重定向到404,通过以下代码
Route::fallback(function(){
return response(json_encode(['code' => 404, 'msg' => '404 not found']), 404);
});
上面出现404的原因就在这里
4、我们需要修改这里的fallback
Route::fallback(function(Request $request){
$response = strtoupper($request->method()) === 'OPTIONS' ? response('', 204) : response(json_encode(['code' => 404, 'msg' => '404 not found']), 404);
$response->withHeaders([
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Allow-Origin' => "*",
'Access-Control-Allow-Methods' => '*',
'Access-Control-Allow-Headers' => '*',
]);
return $response;
});
本文链接:
/archives/1744165907749
版权声明:
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自
雕刻时光!
喜欢就支持一下吧