从JSON响应中删除HTML代码

时间:2014-06-05 13:25:00

标签: html json cakephp

我正在尝试以JSON格式获得响应,但CakePHP还在响应中包含了页面的html代码。 功能如下:

public function register() {
        if ($this->request->is('get')) {
            $dataToSave = array(
                'User' => array(
                    'username' => $this->request->data['User']['username'],
                    'email' => $this->request->data['User']['email'],
                    'password' => $this->request->data['User']['password'],
                    'name' => $this->request->data['User']['name'],
                    'surname' => $this->request->data['User']['surname'],
                    'image_url' => "",
                ),
                'Filter' => array(
                    'Filter' => $this->request->data['Filter']['Filter']
                )
            );
            parent::uploadImageUser();
            $this->User->create();
            $dataToSave['User']['image_url'] = $this->image_path;

            $agent = $this->request->header('User-Agent');

            if ($this->User->saveAll($dataToSave)) {
                $this->set(compact("response", "success"));
                $this->set("_serialize", array("response"));
                $this->Session->setFlash(__('The user has been saved'));
                if ($agent != "FoodAdvisor client/Android"){                    
                    $this->redirect($this->Auth->redirect(array('controller' => 'users', 'action'=>'index')));
                }
            } 

            else {
                $this->set("response", "error");
                $this->set("_serialize", array("response"));
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }

        else {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect(array('action'=>'index')));
            }
        }
    }

我得到的回应是这样的: enter image description here

如何从我的回复中删除html代码?

2 个答案:

答案 0 :(得分:1)

您需要确保'用户' index是在27到31之间的行上定义的,' Filter' index在您文件的第35行定义。可能$this->request->data不包含用户和过滤器数组。

为什么不这样:$dataToSave = $this->request->data;

答案 1 :(得分:0)

if ($this->request->is('get'))行之后

尝试打印$this->request->data的内容,即pr($this->request->data)。并检查数组中是否有“用户”索引。您得到的错误可能是由于缺少数组中的“用户”索引。