简体   繁体   中英

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!!!

我在我的代码中发现了大错误,我在从函数中返回数据之后在我的函数中使用了dd ,并且dd包装了我的响应。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM