【问题标题】:CakePHP 1.3 - json REST call not displaying correctlyCakePHP 1.3 - json REST 调用未正确显示
【发布时间】:2012-02-29 19:07:34
【问题描述】:

我按照this tutorial 为 JSON 和 XML 设置了我的 REST Web 服务。 XML 输出正确,但是当我进行 JSON 调用时,我从 Cake 中得到视图未找到显示。

为此,我在 AppController 中添加了以下代码:

     if ( $this->RequestHandler->isAjax() ) {
        //Configure::write ( 'debug', 0 );
        $this->layout = 'ajax';
        $this->autoRender = false;      
      } elseif  ($this->RequestHandler->isXml()) {
        $this->layout = 'default';      
        //Configure::write ( 'debug', 0 );
      } elseif ($this->RequestHandler->ext == 'json') { 
        $this->RequestHandler->setContent('json','text/x-json');  
        $this->layout = 'default';      
      } elseif ($this->RequestHandler->accepts('html')) {
        $this->layout = 'frontend';     
      }

这是我的控制器方法之一中的代码示例:

      if ($this->RequestHandler->isXml()) {
        $voicenote = $voicenote['Voicenote'];
        $this->set(compact('voicenote'));
      } else if ($this->RequestHandler->ext == 'json') {
        $voicenote = $voicenote['Voicenote'];
        pr($voicenote);
        echo json_encode(array('voicenote' => $voicenote));                                                                                                                                             
      } else {
        $this->set(compact('voicenote', 'tiny_list'));                                                                                                                                                  
      }

XML 显示正常,只是 JSON 是问题所在。

【问题讨论】:

  • 您是使用 AJAX 请求测试 json,还是只是在浏览器中输入 URL?
  • 只是 URL,我得到的视图没有找到 CakePHP 默认视图,但我知道它正在通过正确的渠道,因为我打印出调用的结果,我可以看到 JSON 数组
  • JSON 显示不正确怎么办? application/json 是 JSON 的标准内容类型,而不是 text/x-json

标签: xml json cakephp rest cakephp-1.3


【解决方案1】:

问题是只有在通过 AJAX 请求执行请求时才会禁用自动渲染。 在浏览器地址栏中输入地址时,在echo json_encode(); 调用之后,控制器将继续在渲染管道中,寻找要输出的动作和布局模板。


我建议在您的 XML 和 JSON 渲染之间保持一致,并通过模板文件输出,而不是禁用 AJAX 请求的自动渲染。

应用控制器:

if  ($this->RequestHandler->isXml()) { // Allow a json request to specify XML formatting
  $this->RequestHandler->respondAs('xml'); // for setting headers
  $this->RequestHandler->renderAs($this, 'xml'); // for specifying templates for rendering
} elseif ($this->RequestHandler->ext == 'json'|| $this->RequestHandler->isAjax()){ // 'action' ajax requests and all 'action.json' requests receive JSON
  $this->RequestHandler->respondAs('json');
  $this->RequestHandler->renderAs($this, 'json');
}
// Other requests will fall through to the default HTML rendering

你的控制器只需要设置数据,每个视图模板都会根据需要进行格式化:

function action() {
  // ...
  $voicenote = $voicenote['Voicenote'];
  $this->set(compact('voicenote', 'tiny_list'));                                                                                
}

JSON 模板会比较简单,根据错误信息的要求创建。
布局:

<?php echo $content_for_layout; ?>

JSON 动作(app/views/控制器/json/动作.ctp)

<?php echo json_encode(array('voicenote' => $voicenote)); ?>

查看CakePHP RequestHandler documentation 获取更多帮助

【讨论】:

  • 在 RenderAs 行致命错误:无法通过第 35 行 /Applications/XAMPP/xamppfiles/htdocs/fonykweb/app/app_controller.php 中的引用传递参数 1
  • 我错过了 renderAs() 需要将控制器作为第一个参数传递,所以正确的调用是 renderAs($this, 'json')。我也更新了答案中的代码。
  • +1 对我也很有用,谢谢分享 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2014-08-06
  • 2011-09-12
相关资源
最近更新 更多