vladimir
vladimir

Reputation: 705

Laravel 5 response json

I want to return simple json from my controller.

This code returns me json, but wrapped with html tags and script:

public function getSomething($request) {
       return response()->json(['name' => 'Abigail', 'state' => 'CA']);
    }

But when I use this code:

public function getSomething($request) {
       return json_encode(['name' => 'Abigail', 'state' => 'CA']);
    }

I get clear json response without any html tags.

How can I turn off some html wrapper in the first code, I need only clear json response from my function.

Thanks in advance.

I think problem in chrome not in laravel response, it adds some wrap to my response

Added: No I have the same problem in ie too, and my second code wrapped with html too, I can't understand what is the problem. Anybody can help me? Thanks.

Added: OK when I use die in my function all works fine for me

public function getSomething($request) {
           die json_encode(['name' => 'Abigail', 'state' => 'CA']);
        }

but when I use return json_encode(['name' => 'Abigail', 'state' => 'CA']); laravel wrap my response with html and with js script... something like this:

<script> Sfdump = window.Sfdump...

I have found the BIG mistake in my code, I used dd in one my function after return the data from it, and dd wrap my response. So... my bug is resolved!!!

Upvotes: 1

Views: 3348

Answers (1)

vladimir
vladimir

Reputation: 705

I have found the BIG mistake in my code, I used dd in one my function after return the data from it, and dd wrap my response.

Upvotes: 3

Related Questions