Joomla组件:控制器未返回json [英] Joomla component: controller not returning json
问题描述
为什么我的控制器不返回JSON格式的数据?请注意,我正在Joomla 3.1.1上开发组件.
Why is my controller not returning my data in JSON format? Note that I am developing my component on Joomla 3.1.1.
/hmi.php
//Requries the joomla's base controller
jimport('joomla.application.component.controller');
//Create the controller
$controller = JControllerLegacy::getInstance('HMI');
//Perform the Request task
$controller ->execute(JRequest::setVar('view', 'hmimain'));
//Redirects if set by the controller
$controller->redirect();
/controller.php
class HMIController extends JControllerLegacy
{
function __construct()
{
//Registering Views
$this->registerTask('hmimain', 'hmiMain');
parent::__construct();
}
function hmiMain()
{
$view =& $this->getView('hmimain','html');
$view->setModel($this->getModel('hmimain'), true);
$view->display();
}
public function saveHMI()
{
echo 'Testing';
$this->display();
}
}//End of class HMIController
/controllers/properties.json.php
class HMIControllerProperties extends JController
{
function __construct()
{
$this->registerTask(' taskm', 'taskM');
parent::__construct();
}
function taskM()
{
$document =& JFactory::getDocument();
// Set the MIME type for JSON output.
$document->setMimeEncoding('application/json');
// Change the suggested filename.
JResponse::setHeader('Content-Disposition','attachment;filename="json.json"');
echo json_encode('Hello World');
// Exit the application.
Jexit();
}
}
JQuery函数调用joomla任务
JQuery function calling the joomla task
var request = $.ajax({
dataType:"json",
url:"index.php?option=com_hmi&task=properties.taskm&format=json",
type:"POST",
data:{propPage: "ABC"},
beforeSend: function (){
$("#loading_Bar").css("display","block");
}
});// dot ajax
当我使用上述ajax设置时,请求失败.但是,如果我将datatype属性更改为text,然后从url中删除format=json
,则会得到html而不是json.
When I use the above ajax settings the request fails. However if I change the datatype property to text, and remove the format=json
from the url, I get html instead of json.
有人可以指出我做错了什么吗?
Can some one point out what I'm doing wrong?
推荐答案
进一步研究我的问题,我得出结论,由于我的/hmi.php
Further investigation to my problem i concluded that the componenet was not triggering the desired task becuse of the following code in my /hmi.php
$ controller-> execute(JRequest :: setVar('view','hmimain'));
$controller ->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 文件
then created the /controllers/properties.json.php file with the following code
class HMIControllerProperties extends JControllerLegacy
{
function myMethod()
{
$model = $this->getModel('hmimain');
$dataToolboxItems =& $model->getToolboxItems();
echo json_encode($dataToolboxItems);
//JExit();
}
}//End of class HMIController
然后我从jquery调用任务,如下所示:
then i'm calling the task from jquery as follows:
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");
}
});
这篇关于Joomla组件:控制器未返回json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!