贝洛夫是我的代码,我想从控制器传递输出来查看,现在我的问题是,当我做代码这$errormsg = $this->_setError('f_001'); $this->_view->set('errormsg', $errormsg);
它工作在索引(),但不是在processjobsearch()。传递输出从控制器查看
processjobsearch()函数我正在通过ajax调用,我该如何设置输出以查看现在,哪里出错?请谁能帮助我..
**调用函数在工作/ index.tpl里**
function jobsearch()
{
var form=$("#jobSearchForm")
//$("#result").html("<img alt="ajax search" src='ajax-loader.gif'/>");
$.ajax({
type: 'POST',
url: '/jobs/processjobsearch/',
data: form.serialize(),
success: function(data){
alert(data);
//document.getElementById("display").innerHTML = data;
$.each(data, function(index, value) { alert(value);
$.each(value, function(index, value) {
$("#data").append("<tr><td>" + value + '</td></tr>');
});
});
}
});
}
//accessing value like this
<?php echo $errormsg; ?> //this gives me output if called from index()
//but doesn't give output when called from processjobsearch()
全球视野
class View
{
protected $_file;
protected $_data = array();
public function __construct($file)
{
$this->_file = $file;
}
public function assign($variable , $value)
{
$this->data[$variable] = $value;
}
public function set($key, $value)
{
$this->_data[$key] = $value;
}
public function get($key)
{
return $this->_data[$key];
}
public function output()
{
if (!file_exists($this->_file))
{
throw new Exception("View " . $this->_file . " doesn't exist.");
}
extract($this->_data);
ob_start();
include($this->_file);
$output = ob_get_contents();
ob_end_clean();
echo $output;
}
}
全局控制器
class Controller
{
protected $_model;
protected $_controller;
protected $_action;
protected $_view;
protected $_modelBaseName;
protected $cookName;
//protected $_data = array();
public function __construct($model, $action)
{
$this->_controller = ucwords(__CLASS__);
$this->_action = $action;
$this->_modelBaseName = $model;
$this->_view = new View('views' . DS . strtolower($this->_modelBaseName) . DS . $action . '.tpl');
}
protected function _setModel($modelName)
{
$modelName .= 'Model';
$this->_model = new $modelName();
}
protected function _setView($viewName)
{
$this->_view = new View('views' . DS . strtolower($this->_modelBaseName) . DS . $viewName . '.tpl');
}
public function _setError($errorCode)
{ //echo "pass1";
$errormsg = $this->_model->getError($errorCode); //echo $errormsg [jserr_msg];
return $errormsg [jserr_msg];
}
}
jobscontroller
class JobsController extends Controller
{
public function __construct($model, $action)
{
parent::__construct($model, $action);
$this->_setModel($model);
}
public function index()
{
$this->_view->set('title', 'Job Search');
$script2 = BASE_FRONT.'js/jquery-1.11.3.min.js';
$script4 = BASE_FRONT.'js/scw.js';
$js_global = array($script2, $script4);
$this->_view->set('js_global', $js_global);
$errormsg = $this->_setError('f_001');
$this->_view->set('errormsg', $errormsg); //in index function it works
return $this->_view->output();
}
/**
* Defines processjobsearch, called from jobs/index.tpl file
* it process value to search getJobSearchData function in jobsinmodel to get relative detail.
*/
public function processjobsearch()
{
try {
$valToSearch = isset($_POST['keyword']) ? trim($_POST['keyword']) : NULL;
$nature = isset($_POST['nature']) ? trim($_POST['nature']) : NULL;
$jobs = $this->_model->getJobSearchData($valToSearch, $nature);
// print_r($jobs);
$errormsg = $this->_setError('f_001');
$this->_view->set('errormsg', $errormsg); //here it doesn't works
$this->_view->set('jobs', $jobs);
// return $this->_view->output();
} catch (Exception $e) {
echo '<h1>Application error:</h1>' . $e->getMessage();
}
}
}
我已经编辑我的问题我已经忘了补充问题该功能,但它确实存在DER – Durga
'/ processjobsearch.tpl'没有文件,它是一个功能,所以怎么能我将它更改为'/ processjobsearch.json',并且我尝试了你的代码,它给了我这个错误'JSON.parse:意外的字符在JSON数据的第1行第1列' – Durga
我知道'processjobsearch.tpl' doesn'实际上还存在,但我假设了一个基本的命名约定。也就是说,如果'JobController :: index()'有模板'jobs/index.tpl','JobController :: processjobsearch()'的模板将是'jobs/processjobsearch.tpl'。我建议通过AJAX调用'processjobsearch()'方法,结果应该是JSON。然后你可以用JS分离出作业数据和错误数据。上面的代码不应该只是工作,而是让你走上正确的轨道。 – PiranhaGeorge