为什么我的控制器没有以 JSON 格式返回我的数据?请注意,我正在 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();
/控制器.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();
}
}
调用 joomla 任务的 JQuery 函数
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 设置时,请求失败。但是,如果我将数据类型属性更改为文本,并format=json
从 url 中删除,我会得到 html 而不是 json。
有人能指出我做错了什么吗?