我正在使用 symfony 1.4,以 propel 作为 ORM 创建我的项目。我想在调用 url 时获得 JSON 格式的响应。我已将 header 设置为“application/json”,但它不起作用,我以 HTML 格式返回响应..我无法解码。我们如何在 symfony 中设置内容类型?
示例代码:
行动-
public function executeIndex(sfWebRequest $request)
{
$data_array=array("name" => "harry", "mobile" => "9876543210");
$data_json=json_encode($data_array);
$this->output=$data_json;
}
看法-
<?php
header('Content-type: application/json');
echo $output;
?>
最佳答案
好的,我知道我出错的地方了..yhe 代码应该是..
行动-
public function executeIndex(sfWebRequest $request)
{
$this->getResponse()->setContentType('application/json');
$data_array=array("name" => "harry", "mobile" => "9876543210");
$data_json=json_encode($data_array);
return $this->renderText($data_json);
}
此代码对我有用,如果您有更好的解决方案,请发布..
关于json - 在 symfony 中将内容类型设置为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4021229/