barryvdh / laravel-debugbar

Debugbar for Laravel (Integrates PHP Debug Bar)
MIT License
17.33k stars 1.56k forks source link

Unparsable JSON response with laravel-debugbar #1383

Open Fumiya-Kai opened 1 year ago

Fumiya-Kai commented 1 year ago

When I use fetch of javascript, I kept seeing Uncaught (in promise) SyntaxError: Unexpected non-whitespace character after JSON at position ~.

How to reproduce

environment

MacOS 11.3.1 chrome 109.0.5414.87 php 8.0.18 laravel 9.45.1 laravel-debugbar 3.7

what I did

  1. Enable laravel-debugbar DEBUGBAR_ENABLED = TRUE

※if disable laravel-debugbar, I succeeded. DEBUGBAR_ENABLED = FALSE

  1. Make Api

    Route::get('/test', function () {
    return response(json_encode(['test' => 'aaa']));
    });
  2. Fetch using js

    fetch(location.origin + '/api/test')
    .then(response => response.json())
    .then(data => console.log(data));
  3. Start server as follows php artisan serve

  4. I failed fetch because of the following error. Uncaught (in promise) SyntaxError: Unexpected non-whitespace character after JSON at position ~

    スクリーンショット 2023-01-28 11 59 22

Expected

I expected response data is shown in console as follows.

スクリーンショット 2023-01-28 14 03 23

laravel-debugbar inject code after response data.

% curl http://127.0.0.1:8001/api/test
{"test":"aaa"}<link rel='stylesheet' type='text/css' property='stylesheet' href='//127.0.0.1:8001/_debugbar/assets/stylesheets?v=1657531602&theme=auto' data-turbolinks-eval='false' data-turbo-eval='false'><script src='//127.0.0.1:8001/_debugbar/assets/javascript?v=1657531602' data-turbolinks-eval='false' data-turbo-eval='false'>...

I propose stopping inject code when JSON response.

barryvdh commented 1 year ago

If you use the Json response, it sets the correct headers and doesn't inject the debugbar.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this issue is still present on the latest version of this library on supported Laravel versions, please let us know by replying to this issue so we can investigate further. Thank you for your contribution! Apologies for any delayed response on our side.