laravel-debugbar icon indicating copy to clipboard operation
laravel-debugbar copied to clipboard

Unparsable JSON response with laravel-debugbar

Open Fumiya-Kai opened this issue 2 years ago • 2 comments

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']));
});
  1. Fetch using js
fetch(location.origin + '/api/test')
.then(response => response.json())
.then(data => console.log(data));
  1. Start server as follows php artisan serve

  2. 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.

Fumiya-Kai avatar Jan 28 '23 06:01 Fumiya-Kai

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

barryvdh avatar Feb 22 '23 19:02 barryvdh

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.

stale[bot] avatar Jun 18 '23 19:06 stale[bot]