将异步javascript post请求合并到php类文件中
我继承了一个名为syndrome的模糊PHP框架网站,我找不到任何文档,但对于一个好的PHP开发人员来说,我试图解决的问题应该相当简单 我试图从javascript向php文件发出ajax请求,以执行特定的函数。ajax请求很简单:将异步javascript post请求合并到php类文件中,javascript,php,jquery,ajax,post,Javascript,Php,Jquery,Ajax,Post,我继承了一个名为syndrome的模糊PHP框架网站,我找不到任何文档,但对于一个好的PHP开发人员来说,我试图解决的问题应该相当简单 我试图从javascript向php文件发出ajax请求,以执行特定的函数。ajax请求很简单: loadNewImage = function(){ $.ajax({ url: '/app/library/Controller/Reel.php', data: {action: 'test'},
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);
}
}
然后在ControllerSite.php扩展的Controller.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;
}
试试这个:
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);
}
}
}
尝试创建名为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请求中,您试图发送带有测试值的操作参数,我认为框架的职责是调用名为“test”的相关方法
这可能会有危险。运气不好!我得到一个500的错误。可能是响应对象不能容纳一个简单的字符串,可能…?@mheavers:为什么不跟踪您的500错误,看看可能是什么问题?请先查看您的日志。@mheavers我找不到任何对syndrome PHP框架的引用?!你确定是这样吗?是的,是这样,但它可能是前一位开发人员正在构建的专有框架,因此属于此类。在这种情况下,你可以查看BaseController_Web类,并检查SetResponseEyeah中正在发生和预期的事情。我想我一直坚持的是如何调用该方法,而不是在方法本身内部做什么。
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);
}
}
}
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);
}