Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml CakePHP 1.3-json REST调用未正确显示_Xml_Json_Cakephp_Rest_Cakephp 1.3 - Fatal编程技术网

Xml CakePHP 1.3-json REST调用未正确显示

Xml CakePHP 1.3-json REST调用未正确显示,xml,json,cakephp,rest,cakephp-1.3,Xml,Json,Cakephp,Rest,Cakephp 1.3,我随后为JSON和XML设置了REST Web服务。XML输出正确,但当我调用JSON时,我从Cake中获得视图NotFound display 为此,我在AppController中添加了以下代码: if ( $this->RequestHandler->isAjax() ) { //Configure::write ( 'debug', 0 ); $this->layout = 'ajax'; $this->a

我随后为JSON和XML设置了REST Web服务。XML输出正确,但当我调用JSON时,我从Cake中获得视图NotFound display

为此,我在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()) { // 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
这是我的一个控制器方法中的代码示例:

      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请求执行请求时,才会禁用自动渲染。 在浏览器地址栏中输入地址时,在
echo json_encode()之后调用,控制器将在渲染管道中继续,查找要输出的操作和布局模板


我建议您在XML和JSON呈现之间保持一致,并通过模板文件进行输出,而不要禁用AJAX请求的autoRender

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()) { // 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模板将相对简单,根据错误消息的要求创建。
布局:


JSON操作(app/views/controller/JSON/Action.ctp)




查看以获取更多帮助

您是使用AJAX请求测试json,还是只是在浏览器中输入URL?仅URL,我获得的视图为not found CakePHP默认视图,但我知道它通过了正确的通道,因为我打印了调用结果,并且我可以看到json阵列json如何没有正确显示
application/json
是json的标准内容类型,而不是
text/x-json
。在RenderAs行上出现此错误致命错误:无法在第35I行的/Applications/XAMPP/xamppfiles/htdocs/fonykweb/app/app_controller.php中通过引用传递参数1。RenderAs()需要将控制器作为第一个参数传递,因此正确的调用是renderAs($this,'json')。我也更新了答案中的代码。+1对我也很有用,谢谢分享:)