【问题标题】:Laravel 4: JSON Response is being outputted in the html bodyLaravel 4:在 html 正文中输出 JSON 响应
【发布时间】:2015-02-04 20:01:09
【问题描述】:

我有一个资源丰富的控制器,想为我的 API 响应添加另一个路由。但似乎当我在浏览器中输入 localhost/mylaravel/api/updates 时,它确实显示输出不在 application/json 内容类型中。我在 firebug 中查看并验证了 JSON 已添加到用

 标签包裹的 html 正文中。如何解决此类问题?

routes.php

 Route::group(array('prefix' => 'api'), function() {

        Route::get('updates', 'PluginsController@updatesNotification');

});

Route::resource('plugins', 'PluginsController');

插件控制器.php

class PluginsController extends BaseController {
public function index() { ... }
public function create() { ... }
...
...
...
public function updatesNotification() {
        return Response::json(array('name' => 'Steve', 'state' => 'CA'));
    }

【问题讨论】:

    标签: laravel laravel-4


    【解决方案1】:

    我认为这就是以application/json 发送的 JSON 在 Firefox 中呈现的方式。 Response::json 应该以application/json 正确发送。在 Firefox 中,我可以根据字体来区分。尝试创建 test.html 并在其中只放入 JSON,您会发现它是常规字体,周围没有 <pre>

    【讨论】:

      【解决方案2】:
      JSON <pre> tag is displayed in Firebug only.
      
      You can direclty use the output object of Response::json()  to manipulate and render data. 
      

      当我使用 Response::json() 方法返回 AJAX 响应以使用 Jqplot 库绘制图形时,它工作正常。

      My code is 
      
      return View::make('jqplot::line')->with('data', Response::json($tdata));
      

      【讨论】:

        猜你喜欢
        • 2014-11-11
        • 2014-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-07
        • 2014-04-05
        • 2015-01-26
        • 2021-01-24
        相关资源
        最近更新 更多