( ‘joomla.application.component.controller’);
//创建控制器 $ controller = JControllerLegacy :: getInstance(’ HMI </跨度> “);
//执行请求任务 $ controller - &gt; execute(JRequest :: setVar
对我的问题的进一步调查我得出结论,组件并没有触发我所需的以下代码所需的任务 的 /hmi.php 强>
$ controller - &gt; execute(JRequest :: setVar('view','hmimain'));
所以我修改了我的 的 /hmi.php 强> 如下
//Requries the joomla's base controller jimport('joomla.application.component.controller'); // Create the controller $controller = JControllerLegacy::getInstance('HMI'); $selectedTask = JRequest::getVar( 'task'); if ($selectedTask == null) { //This will allow you to access the main view using index?option=com_hmi //and load the "default" view $controller->execute( JRequest::setVar( 'view', 'hmimain' ) ); } else { //Will execute the assigned task $controller->execute( JRequest::getVar( 'task' ) ); } // Redirect if set by the controller $controller->redirect();
然后创建了 的 /controllers/properties.json.php 强> 文件包含以下代码
class HMIControllerProperties extends JControllerLegacy { function myMethod() { $model = $this->getModel('hmimain'); $dataToolboxItems =& $model->getToolboxItems(); echo json_encode($dataToolboxItems); //JExit(); } }//End of class HMIController
然后我从jquery调用任务如下:
var request = $.ajax({ dataType:"json", //task=properties.mymethod will access the subcontroller within the controllers folder //format=json will by access the json version of the subcontroller url:"index.php?option=com_hmi&task=properties.mymethod&format=json", type:"POST", data:{propPage: "ABC"}, beforeSend: function (){ $("#loading_Bar").css("display","block"); } });
在您的ajax请求中尝试更改为以下格式:
dataType:'json', url: 'index.php', data: {option: 'com_hmi', task: 'properties.task', format: 'jason', propPage: 'ABC' }, type:'POST',
.....
另一件事是在控制器文件中添加Legacy: HMIControllerProperties扩展了JControllerLegacy
而且我认为你不需要这一行,对我而言,它没有它们
$document->setMimeEncoding('application/json'); JResponse::setHeader('Content-Disposition','attachment;filename="json.json"');