title: Joomla - Controller task that returns JSON data
body: <p>I have the task <code>run</code> in my controller. I want it to return JSON data. As it stands, I am getting my JSON data wrapped inside the template HTML. How do I tell Joomla to just return JSON data from the controller? This is the function I have:</p>
  
  <pre><code>public function run  ( ) {
  
      JFactory::getDocument()-&gt;setMimeEncoding( 'application/json' );
  
      JResponse::setHeader('Content-Disposition','attachment;filename="progress-report-results.json"');
  
      JRequest::setVar('tmpl','component');
  
      $data = array(
          'foo' =&gt; 'bar'
      );
  
      echo json_encode( $data );
  
  }
  </code></pre>
  
  <p>And this returns:</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr"&gt;
  ...
  &lt;/head&gt;
  &lt;body class="contentpane"&gt;
  
  &lt;div id="system-message-container"&gt;
  &lt;/div&gt;
      {"foo":"bar"}
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I would like to get:</p>
  
  <pre><code>{"foo":"bar"}
  </code></pre>