如何在CakePHP中为JSON返回正确的内容类型?

如何在CakePHP中为JSON返回正确的内容类型?,php,ajax,json,cakephp,content-type,Php,Ajax,Json,Cakephp,Content Type,我试图为通过AJAX GET请求访问的JSON响应设置内容类型头。我一直关注博客和面包店的教程,但我总是从CakePHP收到“text/html”。如何正确设置内容类型标题 这是我目前的代码: public function admin_controller_actions_ajax() { Configure::write('debug', 0); if ($this->RequestHandler->isGet()) { $this->aut

我试图为通过AJAX GET请求访问的JSON响应设置内容类型头。我一直关注博客和面包店的教程,但我总是从CakePHP收到“text/html”。如何正确设置内容类型标题

这是我目前的代码:

public function admin_controller_actions_ajax()
{
    Configure::write('debug', 0);
    if ($this->RequestHandler->isGet()) {
        $this->autoRender = FALSE;

        $aco_id = $this->params['url']['aco_id'];
        $aro_id = $this->params['url']['aro_id'];

        assert('$aco_id != NULL && $aro_id != NULL &&
                is_numeric($aco_id) && is_numeric($aro_id)');

        $actions = $this->Resource->getActionsForController($aco_id, $aro_id);

        assert('is_array($actions) && is_array($actions[0])');

        /* I made RequestHandler part of the $components property */
        $this->RequestHandler->setContent('json');
        $this->RequestHandler->respondAs('json'); /* I've tried 'json', 'JSON', 'application/json' but none of them work */

        $this->set('json_content', json_encode(array('response' => $actions[0])));
        $this->layout = NULL;
        $this->render('/json/default');
    }
}


/* json/default.ctp */
<?php echo $json_content; ?>
public function admin\u controller\u actions\u ajax()
{
Configure::write('debug',0);
如果($this->RequestHandler->isGet()){
$this->autoRender=FALSE;
$aco_id=$this->params['url']['aco_id'];
$aro_id=$this->params['url']['aro_id'];
断言('$aco_id!=NULL&$aro_id!=NULL&&
是数字($aco_id)&&is_数字($aro_id);
$actions=$this->Resource->getActionsForController($aco\u id,$aro\u id);
断言('is_数组($actions)&&is_数组($actions[0]);
/*我使RequestHandler成为$components属性的一部分*/
$this->RequestHandler->setContent('json');
$this->RequestHandler->respondAs('json');/*我尝试了'json','json','application/json',但都不起作用*/
$this->set('json_content',json_encode(数组('response'=>$actions[0]));
$this->layout=NULL;
$this->render('/json/default');
}
}
/*json/default.ctp*/
任何帮助都将不胜感激

谢谢

--
Isaac

我不确定(老实说,我从未使用过CakePHP),但您可能希望尝试在setContent方法中指定第二个参数

替换此项:

$this->RequestHandler->setContent('json') 
为此:

$this->RequestHandler->setContent('json', 'text/x-json');

请参见示例。

我在所有项目中调用Ajax来检索JSON内容,而您在这里所做的大部分工作我都从未做过。控制器代码的范围如下所示:

public function do_something_ajaxy() {
  Configure::write ( 'debug', 0 );
  $this->autoRender = false;

  /** Business logic as required */

  echo json_encode ( $whatever_should_be_encoded );
}
我通过jQuery调用Ajax,所以我认为这可能会有所不同,但这会让我感到惊讶。在本例中,您的问题似乎出现在处理程序中,而不是调用方。我建议删除第17-23行,并用一个简单的
echo json_编码(array('response'=>$actions[0])
语句替换它们


您还测试了
$this->RequestHandler->isGet()
。尝试测试
$this->RequestHandler->isAjax()
。我不确定Ajax调用是否可以通过其类型和方法识别出来。

我也遇到了这个问题,并通过使用以下方法解决了这个问题:

$this->RequestHandler->respondAs('text/x-json');

还要确保配置文件中的“debug”设置为小于2,否则将不会设置标题。

我一直遇到与原始海报相同的问题,对我有效的方法是遵循Rob Wilkerson的建议,但还要确保我使用了

jQuery.ajax()
而不是

jQuery.get()

ajax()允许您将数据类型设置为“json”,而另外两个似乎根本不允许您设置数据类型。当我将AJAX请求中的数据类型设置为“json”时,一切都正常工作。

在阅读和之后,我得到以下返回“内容类型:application/json”:

使用JQuery的$.getJSON方法,我仍然可以

Resource interpreted as image but transferred with MIME type text/html.

但至少我的数据正在解析。

在routes.php文件中是否使用Router::parseExtensions()?如果是,您必须在app/config/core.php中将“debug”设置为0,以使其以正确的内容类型响应。不,我没有在请求中使用Router::parseExtensions(),但我还是在请求中将debug设置为0。我以前尝试过这样做-我只使用了“return json_encode”(我也尝试了“echo json_encode”)。IRC频道告诉我,这是一种不好的方式,正确的方式是使用视图。我不同意,但我还是照办了。我将测试isAjax,但考虑到我的ajax调用是一个get请求,isGet也应该可以工作。我还使用jQuery调用ajax。我刚刚尝试了你的建议(我简化了所有内容,以echo json_编码,并使用isAjax而不是isGet),但仍然返回了“text/html”。对不起,Isaac。我忘了重复这个。只要内容本身是有效的JSON,内容类型就不重要。我避免使用这样的视图,因为这是一个额外的文件,基本上什么都不做。我对杂乱有问题。:-)对我来说,
Configure::write('debug',0)是神奇的酱汁。注意:根据,
Configure::write('debug',1)有效我刚刚尝试过这个(我以前也尝试过类似的二次参数,但我想我应该再试一次),但它不起作用。我仍然得到“text/html”返回。我也是。我使用它并尝试强制响应类型:$this->RequestHandler->respondAs('json');这两种方法都不适用于我。实际上,您可以在
.post()
.get()
中指定数据类型,请参见文档(从2011年12月起)。我最近在Cake 2.0中使用了此方法(尽管将此代码移动到AppController中的一个函数中,以便任何控制器都可以不重复地使用它)。它似乎工作得很好,允许jQuery(1.7)自动将内容检测为JSON。然而据我所知,
$this->RequestHandler->respondAs('json')
应该是正确的-这对应于设置
application/json
的头,它是
Configure::write('debug', 0);
$this->RequestHandler->respondAs('json');
$this->autoRender = false;            
echo json_encode($data);
Resource interpreted as image but transferred with MIME type text/html.