0

私はcakephp2.1.1を使用しています

そして、私がドキュメントで読んだものから。これにより、RESTAPIを非常に簡単に作成できます。(http://book.cakephp.org/2.0/en/development/rest.html)

JSON応答を生成する方法がわかりません。ObjectiveCからAPIを呼び出していますが、応答は完全なHTMLページです。

私は私のコントローラーにこのコードを持っています

public function add() {
        if ($this->request->is('post')) {
            if ($this->Post->save($this->request->data)) {
                $message = 'Saved';
                //echo 'eyeys';

            } else {
                $message = 'Error';
            }

            $this->set('_serialize',$message);
        }
    }
4

1 に答える 1

1

応答タイプを設定してみてください

public function add() {
    $this->autoRender = false;
    $this->response->type('json');
    $message = null;
        if ($this->request->is('post')) {
            if ($this->Post->save($this->request->data)) {
                $message = 'Saved';
            } else {
                $message = 'Error';
            }
        }        
   return json_encode($message);    
}

公式のcakephp2.0応答ドキュメントもチェックしてください

于 2012-04-25T12:41:43.417 に答える