Joomla组件:控制器不返回json

时间:2013-05-23 13:53:39

标签: joomla components

为什么我的控制器没有以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();

/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();
      }
  }

调用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,我将获得html而不是json。

有人可以指出我做错了吗?

2 个答案:

答案 0 :(得分:1)

对我的问题的进一步调查我得出的结论是,我的 /hmi.php

中的组件并没有触发以下代码所需的任务
  

$ 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 文件

        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");
        }
    });

答案 1 :(得分:0)

在您的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"');