PHP 错误未显示在 Chrome 网络选项卡中(但显示在 Firefox 中)

问题描述 投票:0回答:1

Google Chrome 网络选项卡似乎无法正确显示 PHP 错误。 Firefox 和 Insomnia 完美显示所有错误,但 chrome 只是显示

无法加载响应数据:未找到给定资源的数据 标识符

我尝试过设置标题

'Content-Type: application/json; charset=utf-8'
和很多其他东西,但似乎没有任何效果。

抛出错误的代码:

dd('Test'); // With symfony/var-dumper package
// OR
throw new \Exception('Test');

我的错误处理程序:

    $payload = [
        'error' => [
            'message' => $exception->getMessage(),
            'code' => $exception->getCode()
        ]
    ];

    $response->getBody()->write(json_encode($payload));

    return $response->withHeader('Content-Type', 'application/json')
        ->withStatus(500);

我有什么想法可以做到这一点吗?我使用的是纯 PHP,使用 Laravel 和 Symfony,这完全没有问题。

php google-chrome error-handling google-chrome-devtools
1个回答
0
投票

Chrome 由于 CORS 问题而阻止了该请求:

Access to XMLHttpRequest at 'http://localhost/...' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我通过在

dd('Test')
之前添加以下内容解决了这个问题:

header('Access-Control-Allow-Origin: *');
© www.soinside.com 2019 - 2024. All rights reserved.