Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

User1980's avatar

How to return json with inertia??

Hi all,

How can I return an API json response with intertia please?

controller

       
        return Inertia::render('Dashboard', [
            'data' => $data,
        ]);

vue

            this.$inertia.get('/test-connection', {
                preserveScroll: true,
                onSuccess: (response) => {
                    this.TestConnection = response.data;
                    console.log(response.data);
                },
            })

I am a bit confused on this.

Thanks

0 likes
9 replies
tykus's avatar

Whenever you make an Inertia request you should get an Inertia response (which is JSON). If you need your own JSON data separately from Inertia, then do not make an Inertia request, and do not send an Inertia response

1 like
User1980's avatar

Oh I see, so a simple axios request should be ok then, am I correct?

1 like
User1980's avatar

I am using Laravel Jetstream.

I just tried this:

axios.get('/test-connection')
     .then(response => {
     this.TestConnection = response.data;
     console.log(response);
     });

controller:

        return response()->json($data);

But keep receiving a response like this:

{"cookies":{},"transferStats":{}}

But if I get a logger($data)

I get the correct output which is a json object.

Very weird.

tykus's avatar

Inertia also uses axios under the hood, so perhaps has registered Interceptors that are being executed on your vanilla requests/responses also.

User1980's avatar

Jetstream is a bit tricky to understand....I am wondering if I should instead just have laravel with vue3 and inertia alone....no idea.

karresachi's avatar

Hey, I am also getting response like

{"cookies":{},"transferStats":{}}

Did you solve this? (Also using Jetstream + Inertia)

LaraBABA's avatar

I stopped using it but from what I remember, when I used to get this error, I was not passing the object or array properly to the front end. Try this, first to a logger($data) in the PHP side(controller), check the data there to see if you have an output.

When if you have, replace your return with a string, ie: return response()->json('hello');

See if you get the string in your front end.

If you do, the issue is because you are not accessing the data correctly. In my case if I remember, I had to use something like:

$data[0]['key] or something along these lines....

Hope this helps.

Randy_Johnson's avatar

Just return it as such its already in json format.

$data = Product::all();

return $data;

ignaciodev's avatar

@Randy_Johnson doing this returns an error:

All Inertia requests must receive a valid Inertia response, however a plain JSON response was received.

2 likes

Please or to participate in this conversation.