0

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

有人能指出我做错了什么吗?

4

2 回答 2

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");
        }
    });
于 2013-05-24T13:24:41.603 回答
0

在您的 ajax 请求中尝试更改为这种格式:

dataType:'json',
url: 'index.php',
data: {option: 'com_hmi', task: 'properties.task', format: 'jason', propPage: 'ABC' },
type:'POST',

......

另一件事是在控制器文件中添加 Legacy:HMIControllerProperties extends JControllerLegacy

而且我认为您不需要这些台词,对我来说,没有它们也可以

$document->setMimeEncoding('application/json');
JResponse::setHeader('Content-Disposition','attachment;filename="json.json"');
于 2013-05-23T19:14:25.110 回答