响应内容必须是实现__toString(),\"boolean \"的字符串或对象.给定." [英] The Response content must be a string or object implementing __toString(), \"boolean\" given."
本文介绍了响应内容必须是实现__toString(),\"boolean \"的字符串或对象.给定."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Response::json
返回渲染的视图,但出现此错误:
I'm trying to return a rendered View using Response::json
but I'm getting this error:
The Response content must be a string or object implementing __toString(), \"boolean\" given."
这是我的代码:
$posts = Post::where( ... )->orderBy( ... )->get();
$data['posts'] = View::make("posts.partials.loadHome")->with("posts", $posts)->render();
$data['msg'] = "ok";
return Response::json($data);
如果我var_dump($data)
我明白了:
<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=2)</i>
'posts' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'<div class="post postGrid" data-id="1864"> <a target='_blank' href="http://objavi.net/posts/1864"><img src="http://objavi.net/" id="imgWrap" data-original="/thumbs/YAo4wFzIpl76.jpg" class="lazy" alt="Deset manje poznatih činjenica o Jozefu Staljinu"></a> <div id="bodyPreview"> <a target='_blank' href="http://objavi.net/posts/1864"><h1 class="previewTitle">Deset manje poznatih činjenica o Jozefu Staljinu</h1></a> <h3 id="postInfo"> <a class="paint" href="/category/17">zanimljivosti</a> '...</font> <i>(length=12172)</i>
'msg' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'ok'</font> <i>(length=2)</i>
</pre>
这是posts.partials.loadHome
视图:
@foreach($posts as $post)
<div class="post postGrid" data-id="{{ $post->id }}">
<a target='_blank' href="{{ URL::action('PostsController@show', $post->id) }}">{{ HTML::image(null, $post->title, ["id" => "imgWrap", "data-original" => $post->getThumb(), "class" => "lazy"]) }}</a>
<div id="bodyPreview">
<a target='_blank' href="{{ URL::action('PostsController@show', $post->id) }}"><h1 class="previewTitle">{{ e($post->title) }}</h1></a>
<h3 id="postInfo">
@foreach($post->categories as $c)
<a class="paint" href="/category/{{ $c->id }}">{{ $c->name }}</a>
@endforeach
</h3>
<h2 class="bodyPreview">{{ strip_tags(truncString($post->body, 160)) }}</h2>
<div id="createdBy">
<a href="{{ URL::action('UsersController@show', $post->user()->first()->id) }}">
{{ HTML::image($post->user()->first()->getAvatar(), $post->user()->first()->username, ["width" => "32", "height" => "32"]) }}
{{{ $post->user()->first()->username }}}
</a>
<label id="timeAgo">{{ localDate($post->created_at); }}</label>
</div>
</div>
</div>
@endforeach
我在localhost上进行了测试,一切正常.可能是什么问题?
I tested this on localhost and everything works fine. What could be the problem?
推荐答案
检查以确保没有任何非法字符.我曾经遇到这个问题,然后在字符串上运行了utf8_encode,就解决了这个问题.
Check to make sure there aren't any illegal characters. I had this issue once and ran utf8_encode on the string and that solved the issue.
这篇关于响应内容必须是实现__toString(),\"boolean \"的字符串或对象.给定."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文