响应内容必须是实现__toString(),\"boolean \"的字符串或对象.给定." [英] The Response content must be a string or object implementing __toString(), \"boolean\" given."

查看:75
本文介绍了响应内容必须是实现__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'>=&gt;</font> <small>string</small> <font color='#cc0000'>'&lt;div class=&quot;post postGrid&quot; data-id=&quot;1864&quot;&gt;&#10; &lt;a target=&#39;_blank&#39; href=&quot;http://objavi.net/posts/1864&quot;&gt;&lt;img src=&quot;http://objavi.net/&quot; id=&quot;imgWrap&quot; data-original=&quot;/thumbs/YAo4wFzIpl76.jpg&quot; class=&quot;lazy&quot; alt=&quot;Deset manje poznatih činjenica o Jozefu Staljinu&quot;&gt;&lt;/a&gt;&#10;  &#10;   &lt;div id=&quot;bodyPreview&quot;&gt;&#10;     &#10;       &lt;a target=&#39;_blank&#39; href=&quot;http://objavi.net/posts/1864&quot;&gt;&lt;h1 class=&quot;previewTitle&quot;&gt;Deset manje poznatih činjenica o Jozefu Staljinu&lt;/h1&gt;&lt;/a&gt;&#10;&#10;     &lt;h3 id=&quot;postInfo&quot;&gt;&#10;                         &lt;a class=&quot;paint&quot; href=&quot;/category/17&quot;&gt;zanimljivosti&lt;/a&gt;&#10; '...</font> <i>(length=12172)</i>
  'msg' <font color='#888a85'>=&gt;</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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆