webman异常处理类的render方法的$request参数无法获取到正确的请求头header

hiwork

问题描述

请教下,webman异常处理类的render方法的$request参数无法获取到正确的请求头header,比如前端axios传递的请求头Accept:application/json,但异常处理类的render方法的$request获取的header $request->header()中的accept结果为'星号/星号',这种就导致前端希望返回异常的json数据,但后端不能正确返回json异常。包括前端一些自定义的header也是无法获取到

程序代码或配置

use Webman\Exception\ExceptionHandler;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\Exception\BusinessException;

/**
 * Class Handler
 * @package support\exception
 */
class Handler extends ExceptionHandler
{
    public $dontReport = [
        BusinessException::class,
    ];

    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    public function render(Request $request, Throwable $exception): Response
    {
        dump('系统Handler异常render request header:', $request->header());
        return parent::render($request, $exception);
    }

}

操作系统环境及workerman/webman等具体版本

windows系统
webman版本 2.1

后续

好像找到原因了 是在config/route.php配置文件中 添加如下内容导致的

//当路由不存在时返回一个json数据,这在webman作为api接口时非常实用
Route::fallback(function () {
    dump('路由不存在');
    return json(['code' => 0, 'msg' => 'not found']);
});

上述异常处理类抛出的是PageNotFoundException 为什么加了这句路由 就导致$request获取header异常了呢

后续2

好像跟路由也没关系 我把上述中的路由代码注释了 然后发现通过调试工具ApiPost发送的请求是正常的 但是浏览器通过axios发送的请求 render方法的$request就不能正常获取header了

107 1 0
1个回答

agaegha

浏览器会发送两次请求,一次是预检的OPTIONS,通过了才会发送真正的ajax请求,请求头不一样是不是浏览器都没有通过预检

  • hiwork 5天前

    是的 有可能是跨域的问题 我再看看

🔝