将异步javascript post请求合并到php类文件中

时间:2014-07-27 15:08:40

标签: javascript php jquery ajax post

我继承了一个名为syndrome的模糊PHP框架的网站,我找不到任何文档,但对于一个优秀的PHP开发人员来说,我试图解决的问题应该相当简单。

我正在尝试从javascript向php文件发出ajax请求以执行特定的功能。 ajax请求只是:

loadNewImage = function(){
        $.ajax({ url: '/app/library/Controller/Reel.php',
             data: {action: 'test'},
             type: 'post',
             success: function(output) {
             alert(output);
         }
    });
}

当前的PHP文件结构如下:

<?php

    class Controller_Reel extends BaseController_Web {

        protected function defaultAction() {
            parent::getPage($this->template, 'home');

            $homepage = Homepage::getInstance()->getHomepage();
            $this->template->title = 'Homepage';
            $this->template->image = $homepage['asset_image'];
            $this->template->center = array('reel');
            $this->setResponse($this->template);
        }
    }

我想要做的是在文件中添加对发布数据的检查。我不擅长PHP,但我试过了:

<?php

if(isset($_POST['action']) && !empty($_POST['action'])) {
    echo 'TEST POST';
}

class Controller_Reel extends BaseController_Web {

    protected function defaultAction() {
        parent::getPage($this->template, 'home');

        $homepage = Homepage::getInstance()->getHomepage();
        $this->template->title = 'Homepage';
        $this->template->image = $homepage['asset_image'];
        $this->template->center = array('reel');
        $this->setResponse($this->template);
    }
}

我假设这可能是因为检查发布数据不是在类本身内发生,但我不确定如何构造代码。有人可以帮我理顺吗?


更新:我在名为ControllerSite.php的文件中找到了这个 - &gt; (扩展了baseController_Web:

protected function respond() {
        switch($this->response_type) {
            case self::RESPONSE_PAGE:
                // always try to make ie use the latest rendering engine
            case self::RESPONSE_TEXT:
                Helper_Request::respond($this->processed_response, Helper_Request::RESPONSE_PRINT, Config::$platform);
                break;
            case self::RESPONSE_JSON:
                Helper_Request::respond($this->processed_response, Helper_Request::RESPONSE_JSON, Config::$platform);
                break;
            case self::RESPONSE_REDIR:
                Helper_Request::respond($this->processed_response, Helper_Request::RESPONSE_REDIR, Config::$platform);
                break;
            case self::RESPONSE_CONTENT:
                // TODO: we'll need to figure the out, but don't need to worry about it for now
                break;
        }
        return $this;
    }

然后在Controller.php中(扩展了ControllerSite.php),这个:

final private function execute() {
        $action = $this->getMethodName();
        $is_ajax = Helper_Request::isAjax();
        $data_type = strtolower(Helper_Request::setDefault($_SERVER['HTTP_ACCEPT'], ''));
        if($is_ajax && preg_match('/\w+\/json|\w+\/javascript/i', $data_type) && method_exists($this, $action . 'JsonAction')) {
            // it there was a ajax json request and the ajax json specific method exists, execute it
            return $this->{$action . 'JsonAction'}();

}
return $this;
}

2 个答案:

答案 0 :(得分:0)

试试这个:

class Controller_Reel extends BaseController_Web {

    protected function defaultAction() {
        parent::getPage($this->template, 'home');

        $homepage = Homepage::getInstance()->getHomepage();
        $this->template->title = 'Homepage';
        $this->template->image = $homepage['asset_image'];
        $this->template->center = array('reel');
        if(isset($_POST['action']) && !empty($_POST['action'])) {
            $reponse['success'] = true;
            $response['responseVal'] = 'This is a test';
            $this->setResponse($response);
        } else {
            $this->setResponse($this->template);
        }
    }
}

答案 1 :(得分:0)

尝试创建一个名为testAction的类方法:

    protected function testAction() {
        parent::getPage($this->template, 'home');

        $homepage = Homepage::getInstance()->getHomepage();
        $this->template->title = 'Homepage';
        $this->template->image = $homepage['asset_image'];
        $this->template->center = array('reel');
        $this->setResponse($this->template);
    }

在ajax请求中,您尝试发送带有测试值的action参数,我认为调用以'test'命名的相关方法是框架的职责。

这可能是肝脏。