Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unparsable JSON response with laravel-debugbar #1383

Open
Fumiya-Kai opened this issue Jan 28, 2023 · 2 comments
Open

Unparsable JSON response with laravel-debugbar #1383

Fumiya-Kai opened this issue Jan 28, 2023 · 2 comments
Labels

Comments

@Fumiya-Kai
Copy link

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.

@barryvdh
Copy link
Owner

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

@stale
Copy link

stale bot commented Jun 18, 2023

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 stale bot added the stale label Jun 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants