【问题标题】:Joomla component: controller not returning jsonJoomla 组件:控制器不返回 json
【发布时间】:2013-05-18 22:09:53
【问题描述】:

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

JQuery 函数调用 joomla 任务

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 设置时,请求失败。但是,如果我将数据类型属性更改为文本,并从 url 中删除 format=json,我会得到 html 而不是 json。

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

【问题讨论】:

    标签: joomla components


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复....但是您的解决方案没有运气:/....通过进一步调查,我注意到组件没有触发 controller.php 中的任务。我的 /hmi.php 中的代码不正确
    【解决方案2】:

    对我的问题的进一步调查我得出结论,由于我的 /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-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 2013-04-15
      • 2015-03-24
      相关资源
      最近更新 更多