What's the proper way to serve JSONP with CakePHP?

前端 未结 3 662
南方客
南方客 2020-12-17 02:58

I want to serve JSONP content with CakePHP and was wondering what\'s the proper way of doing it so.

Currently I\'m able to serve JSON content automatically by follow

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-17 03:21

    For CakePHP 2.4 and above, you can do this instead.

    http://book.cakephp.org/2.0/en/views/json-and-xml-views.html#jsonp-response

    So you can simply write:

    $this->set('_jsonp', true);
    

    in the relevant action.

    Or you can simply write:

    /**
     *
     * beforeRender method
     *
     * @return void
     */
        public function beforeRender() {
            parent::beforeRender();
            $this->set('_jsonp', true);
        }
    

提交回复
热议问题