简体   繁体   English

Laravel 5回应json

[英]Laravel 5 response json

I want to return simple json from my controller. 我想从我的控制器返回简单的json。

This code returns me json, but wrapped with html tags and script: 这段代码返回给我json,但用html标签和脚本包装:

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. 我没有任何HTML标签,我得到了明确的json响应。

How can I turn off some html wrapper in the first code, I need only clear json response from my function. 如何在第一个代码中关闭一些html包装器,我只需要清除我的函数中的json响应。

Thanks in advance. 提前致谢。

I think problem in chrome not in laravel response, it adds some wrap to my response 我认为chrome中的问题不在laravel响应中,它为我的响应添加了一些内容

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. 补充: 不,我也有同样的问题,我的第二个代码也用html包裹,我无法理解是什么问题。 Anybody can help me? 有人可以帮帮我吗? Thanks. 谢谢。

Added: OK when I use die in my function all works fine for me 补充:好的,当我在我的功能中使用die时,一切正常

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

but when I use return json_encode(['name' => 'Abigail', 'state' => 'CA']); 但是当我使用return json_encode(['name'=>'Abigail','state'=>'CA']); laravel wrap my response with html and with js script... something like this: laravel用html和js脚本包装我的响应...像这样:

<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. 我在我的代码中发现了大错误,我在从函数中返回数据之后在我的函数中使用了dd,并且dd包装了我的响应。 So... my bug is resolved!!! 所以......我的错误已经解决了!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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