laravel-debugbar
laravel-debugbar copied to clipboard
Unparsable JSON response with laravel-debugbar
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
- Enable laravel-debugbar
DEBUGBAR_ENABLED = TRUE
※if disable laravel-debugbar, I succeeded.
DEBUGBAR_ENABLED = FALSE
- Make Api
Route::get('/test', function () {
return response(json_encode(['test' => 'aaa']));
});
- Fetch using js
fetch(location.origin + '/api/test')
.then(response => response.json())
.then(data => console.log(data));
-
Start server as follows
php artisan serve -
I failed fetch because of the following error.
Uncaught (in promise) SyntaxError: Unexpected non-whitespace character after JSON at position ~
Expected
I expected response data is shown in console as follows.

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.
If you use the Json response, it sets the correct headers and doesn't inject the debugbar.
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.