貝洛夫是我的代碼,我想從控制器傳遞輸出來查看,現在我的問題是,當我做代碼這$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