Joomla component: controller not returning json

故事扮演 提交于 2019-12-12 00:59:33

问题


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 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

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?


回答1:


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'));

So I modified my /hmi.php as follows

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

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

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



回答2:


In your ajax request try changing to this format:

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

.....

Another thing is in the controller file add the Legacy: HMIControllerProperties extends JControllerLegacy

And I don't think you need this lines, for me it works without them

$document->setMimeEncoding('application/json');
JResponse::setHeader('Content-Disposition','attachment;filename="json.json"');


来源:https://stackoverflow.com/questions/16715801/joomla-component-controller-not-returning-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!