MVC: how to ajax?

前端 未结 6 807
我在风中等你
我在风中等你 2021-02-03 14:01

I\'m going to start a project using a Zend Framework MVC implementation.

How do I work with ajax? I mean, should I place all ajax code into controller? Or into view?

6条回答
  •  清歌不尽
    2021-02-03 14:26

    You can utilize the same actions to return XML, JSON or whatever, by detecting ajax requests and thus being able to differentiate ajax requests from normal ones. For example:

    public function fooAction()
    {
        if($this->getRequest->isXmlHttpRequest()) {
            echo json_encode($someData);
        } else {
            echo 'This is the normal output';
        }
    }
    

提交回复
热议问题