沃梦达 / 编程问答 / php问题 / 正文

Response 内容必须是一个字符串或实现 __toString() 的对象,"boolean";给定的."

The Response content must be a string or object implementing __toString(), quot;booleanquot; given.quot;(Response 内容必须是一个字符串或实现 __toString() 的对象,boolean;给定的.)

本文介绍了Response 内容必须是一个字符串或实现 __toString() 的对象,"boolean";给定的."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Response::json 返回呈现的视图,但出现此错误:

响应内容必须是字符串或实现__toString()的对象,"boolean"给定."

这是我的代码:

$posts = Post::where( ... )->orderBy( ... )->get();$data['posts'] = View::make("posts.partials.loadHome")->with("posts", $posts)->render();$data['msg'] = "ok";返回响应::json($data);

如果我 var_dump($data) 我得到这个:

这是 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->标题,[id"=>imgWrap",data-original"=>$post->getThumb(),class"=>懒惰"])}}<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", "高度" => "32"]) }}{{{ $post->user()->first()->username }}}</a><label id="timeAgo">{{ localDate($post->created_at);}}</标签></div></div></div>@endforeach

我在 localhost 上进行了测试,一切正常.可能是什么问题?

解决方案

检查以确保没有任何非法字符.我曾经遇到过这个问题并在字符串上运行 utf8_encode 并解决了这个问题.

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."

This is my code:

$posts = Post::where( ... )->orderBy( ... )->get();
$data['posts'] = View::make("posts.partials.loadHome")->with("posts", $posts)->render();
$data['msg'] = "ok";

return Response::json($data);

If I var_dump($data) I get this:

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

This is posts.partials.loadHome view:

@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

I tested this on localhost and everything works fine. What could be the problem?

解决方案

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.

这篇关于Response 内容必须是一个字符串或实现 __toString() 的对象,"boolean";给定的."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Response 内容必须是一个字符串或实现 __toString() 的对象,"boolean";给定的."

基础教程推荐