我正在使用symfony 1.4,用ORM创建我的推进项目.我想要获得
JSON格式的响应,当我调用一个url.我已经将标题设置为“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;
- ?>
解决方法
好吧,我得到了我错了什么.你的代码应该是..
行动-
行动-
- 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);
- }