<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Chraneco</id>
	<title>Joomla! Documentation - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Chraneco"/>
	<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/Special:Contributions/Chraneco"/>
	<updated>2026-09-17T18:01:06Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=104008</id>
		<title>JSON Responses with JResponseJson</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=104008"/>
		<updated>2013-09-28T20:28:24Z</updated>

		<summary type="html">&lt;p&gt;Chraneco: /* Additional Options */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|3.x}}&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Recently a new class JResponseJson was added to Joomla! 3.x which is able to simplify things with Ajax requests. With that class it is now possible to prepare responses to Ajax requests in a standardized and easy manner. You can have a look at the file here:&lt;br /&gt;
https://github.com/joomla/joomla-cms/blob/master/libraries/cms/response/json.php&lt;br /&gt;
It will mostly be used in controllers of components where you get the following advantages:&lt;br /&gt;
&lt;br /&gt;
* Using the flag &#039;success&#039; the JavaScript code (whether Mootools JRequest.JSON or jQuery.getJSON) can check whether the task was successful or not and react accordingly (If the request itself failes the error event of the JavaScript API can be used).&lt;br /&gt;
* Then, the actual response data (if any) can be retrieved from &#039;data&#039; in the JSON object and&lt;br /&gt;
* optionally a main response message from &#039;message&#039;.&lt;br /&gt;
* Additionally, all gathered messages in the JApplication message queue are also automatically sent back in &#039;messages&#039;. This can optionally be turned off.&lt;br /&gt;
&lt;br /&gt;
Another advantage is that no $app-&amp;gt;close(); is necessary anymore if the Ajax request is done with &#039;format=json&#039; because the already existing API handles the rest. Just echo the response object at the end of the task.&lt;br /&gt;
&lt;br /&gt;
== How to Use ==&lt;br /&gt;
=== Most Common Cases ===&lt;br /&gt;
&lt;br /&gt;
Here is an example controller file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $result = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;createSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($result);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see in the default case the return value of something that has been calculated by the model is simply pushed into a new JResponseJson object and written to the output. This will automatically create a JSON encoded string as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:null,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in the &#039;data&#039; field you can send any array, object or value you want and the &#039;success&#039; flag is automatically set to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
If any exception occured in the model this exception is simply passed directly to a new JResponseJson object in our example which would create the following output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;This is the message of the exception&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:null}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it is an exception the &#039;success&#039; flag was automatically set to &#039;false&#039; and the message of the exception became the main response message.&lt;br /&gt;
&lt;br /&gt;
=== Additional Options ===&lt;br /&gt;
&lt;br /&gt;
If not an exception is passed as the first argument of the JResponseJson constructor you can specify an arbitrary main response message by passing a string as the second argument:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_SUCCESS&#039;));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;The request was successful.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can also manually set the error flag to &#039;false&#039; with the help of the third argument ($error):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_ERROR&#039;), true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;The was an error.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Please note that this way you can also send some data back.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Regardless of having an error response or a success response JResponseJson sends all messages back to the client that have been gathered in the application object:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$app = JFactory::getApplication();&lt;br /&gt;
// Some code ($result = ...)&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;This part was successful&#039;);&lt;br /&gt;
// Some more code&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;Here was a small warning&#039;. &#039;warning&#039;);&lt;br /&gt;
&lt;br /&gt;
echo new JResponseJson($result, &#039;Main response message&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;Main response message&amp;quot;,&amp;quot;messages&amp;quot;:&amp;quot;&amp;lt;&amp;lt;all the encoded messages of $app&amp;gt;&amp;gt;&amp;quot;,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can see the big advantage of that below in the JavaScript section.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t want to send the messages back (rather that they stay in the session), simply set the fourth argument ($ignoreMessages) of the contructor to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Corresponding JavaScript Code ===&lt;br /&gt;
&lt;br /&gt;
Here is some sample JavaScript used which can be used on the client side together with JResponseJson on the server side.&lt;br /&gt;
The example is written with some Mootools code, but something similar can also be done with jQuery or any other JavaScript library for Ajax requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var req = new Request.JSON({&lt;br /&gt;
	method: &#039;post&#039;,&lt;br /&gt;
	url: &#039;index.php?option=com_component&amp;amp;task=mycontroller.execute&amp;amp;format=json&#039;,&lt;br /&gt;
	onSuccess: function(r)&lt;br /&gt;
	{&lt;br /&gt;
		if (!r.success &amp;amp;&amp;amp; r.message)&lt;br /&gt;
		{&lt;br /&gt;
			// Success flag is set to &#039;false&#039; and main response message given&lt;br /&gt;
			// So you can alert it or insert it into some HTML element&lt;br /&gt;
			alert(r.message);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.messages)&lt;br /&gt;
		{&lt;br /&gt;
			// All the enqueued messages of the $app object can simple be&lt;br /&gt;
			// rendered by the respective helper function of Joomla!&lt;br /&gt;
			// They will automatically be displayed at the messages section of the template&lt;br /&gt;
			Joomla.renderMessages(r.messages);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.data)&lt;br /&gt;
		{&lt;br /&gt;
			// Here you can access all the data of your response&lt;br /&gt;
			alert(r.data.myfirstcustomparam);&lt;br /&gt;
			alert(r.data.mysecondcustomparam);&lt;br /&gt;
		}&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onFailure: function(xhr)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request itself was not successful&lt;br /&gt;
		// So JResponseJson was never called&lt;br /&gt;
		alert(&#039;Ajax error&#039;);&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onError: function(text, error)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request was answered by the server, but&lt;br /&gt;
		// the response was no valid JSON (this happens sometimes if there were PHP errors,&lt;br /&gt;
		// warnings or notices during the development process of a new Ajax request).&lt;br /&gt;
		alert(error + &amp;quot;\n\n&amp;quot; + text);&lt;br /&gt;
	}.bind(this)&lt;br /&gt;
});&lt;br /&gt;
req.post(&#039;anyparam=myvalue&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best Practice ==&lt;br /&gt;
As already seen above, code of the controller can be kept very simple using JResponseJson. The model is calculating the data which can be passed to JResponseJson afterwards (of course you can still modify it in the controller if you want to). If you are developing an MVC component you should save such a controller in a file called &#039;mycontroller.json.php&#039; and place it inside your &#039;controllers&#039; folder.&lt;br /&gt;
This way, that controller is automatically executed if your request URL contains &#039;format=json&#039;.&lt;br /&gt;
&lt;br /&gt;
Please note that as you can see in the example, there is no need for closing the application (e.g. $app-&amp;gt;close();) because the architecture of Joomla! handles that for you. So, this is very clean and good programming.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($count);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Additional Note===&lt;br /&gt;
&lt;br /&gt;
If you want to support no-js users by for example doing the same task with a normal request and a final redirect back to the page, there is not much you have do additionally. Just create another controller (now with name &#039;mycontroller.php&#039;) and code like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    $app = JFactory::getApplication();&lt;br /&gt;
    $app-&amp;gt;setRedirect(JRoute::_(&#039;index.php?option=com_mycomponent&amp;amp;view=myview&#039;, false));&lt;br /&gt;
&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::plural(&#039;COM_COMPONENT_COUNT_TASK&#039;, $count));&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::sprintf(&#039;COM_COMPONENT_COUNT_TASK_ERROR&#039;, $e-&amp;gt;getMessage()), &#039;error&#039;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No changes necessary in the model!&lt;br /&gt;
&lt;br /&gt;
So depending on whether you do the request with &#039;format=json&#039; (Ajax-Request) or via normal request (without &#039;format&#039; parameter), the correct controller is executed automatically and the response is prepared accordingly.&lt;br /&gt;
&lt;br /&gt;
[[Category:Extension development]]&lt;br /&gt;
[[Category:Component Development]]&lt;br /&gt;
[[Category:AJAX]]&lt;/div&gt;</summary>
		<author><name>Chraneco</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=104007</id>
		<title>JSON Responses with JResponseJson</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=104007"/>
		<updated>2013-09-28T20:26:59Z</updated>

		<summary type="html">&lt;p&gt;Chraneco: /* Most Common Cases */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|3.x}}&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Recently a new class JResponseJson was added to Joomla! 3.x which is able to simplify things with Ajax requests. With that class it is now possible to prepare responses to Ajax requests in a standardized and easy manner. You can have a look at the file here:&lt;br /&gt;
https://github.com/joomla/joomla-cms/blob/master/libraries/cms/response/json.php&lt;br /&gt;
It will mostly be used in controllers of components where you get the following advantages:&lt;br /&gt;
&lt;br /&gt;
* Using the flag &#039;success&#039; the JavaScript code (whether Mootools JRequest.JSON or jQuery.getJSON) can check whether the task was successful or not and react accordingly (If the request itself failes the error event of the JavaScript API can be used).&lt;br /&gt;
* Then, the actual response data (if any) can be retrieved from &#039;data&#039; in the JSON object and&lt;br /&gt;
* optionally a main response message from &#039;message&#039;.&lt;br /&gt;
* Additionally, all gathered messages in the JApplication message queue are also automatically sent back in &#039;messages&#039;. This can optionally be turned off.&lt;br /&gt;
&lt;br /&gt;
Another advantage is that no $app-&amp;gt;close(); is necessary anymore if the Ajax request is done with &#039;format=json&#039; because the already existing API handles the rest. Just echo the response object at the end of the task.&lt;br /&gt;
&lt;br /&gt;
== How to Use ==&lt;br /&gt;
=== Most Common Cases ===&lt;br /&gt;
&lt;br /&gt;
Here is an example controller file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $result = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;createSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($result);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see in the default case the return value of something that has been calculated by the model is simply pushed into a new JResponseJson object and written to the output. This will automatically create a JSON encoded string as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:null,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in the &#039;data&#039; field you can send any array, object or value you want and the &#039;success&#039; flag is automatically set to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
If any exception occured in the model this exception is simply passed directly to a new JResponseJson object in our example which would create the following output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;This is the message of the exception&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:null}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it is an exception the &#039;success&#039; flag was automatically set to &#039;false&#039; and the message of the exception became the main response message.&lt;br /&gt;
&lt;br /&gt;
=== Additional Options ===&lt;br /&gt;
&lt;br /&gt;
If not an exception is passed as the first argument of the JResponseJson constructor you can specify an arbitrary main response message by passing a string as the second argument:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_SUCCESS&#039;));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;The request was successful.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can also manually set the error flag to &#039;false&#039; with the help of the third argument ($error):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_ERROR&#039;), true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;The was an error.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Please note that this way you can also send some data back.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Regardless of having an error response or a success response JResponseJson sends all messages back to the client that have been gathered in the application object:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$app = JFactory::getApplication();&lt;br /&gt;
// Some code ($result = ...)&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;This part was successful&#039;);&lt;br /&gt;
// Some more code&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;Here was a small warning&#039;);&lt;br /&gt;
&lt;br /&gt;
echo new JResponseJson($result, &#039;Main response message&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;Main response message&amp;quot;,&amp;quot;messages&amp;quot;:&amp;quot;&amp;lt;&amp;lt;all the encoded messages of $app&amp;gt;&amp;gt;&amp;quot;,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can see the big advantage of that below in the JavaScript section.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t want to send the messages back (rather that they stay in the session), simply set the fourth argument ($ignoreMessages) of the contructor to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Corresponding JavaScript Code ===&lt;br /&gt;
&lt;br /&gt;
Here is some sample JavaScript used which can be used on the client side together with JResponseJson on the server side.&lt;br /&gt;
The example is written with some Mootools code, but something similar can also be done with jQuery or any other JavaScript library for Ajax requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var req = new Request.JSON({&lt;br /&gt;
	method: &#039;post&#039;,&lt;br /&gt;
	url: &#039;index.php?option=com_component&amp;amp;task=mycontroller.execute&amp;amp;format=json&#039;,&lt;br /&gt;
	onSuccess: function(r)&lt;br /&gt;
	{&lt;br /&gt;
		if (!r.success &amp;amp;&amp;amp; r.message)&lt;br /&gt;
		{&lt;br /&gt;
			// Success flag is set to &#039;false&#039; and main response message given&lt;br /&gt;
			// So you can alert it or insert it into some HTML element&lt;br /&gt;
			alert(r.message);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.messages)&lt;br /&gt;
		{&lt;br /&gt;
			// All the enqueued messages of the $app object can simple be&lt;br /&gt;
			// rendered by the respective helper function of Joomla!&lt;br /&gt;
			// They will automatically be displayed at the messages section of the template&lt;br /&gt;
			Joomla.renderMessages(r.messages);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.data)&lt;br /&gt;
		{&lt;br /&gt;
			// Here you can access all the data of your response&lt;br /&gt;
			alert(r.data.myfirstcustomparam);&lt;br /&gt;
			alert(r.data.mysecondcustomparam);&lt;br /&gt;
		}&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onFailure: function(xhr)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request itself was not successful&lt;br /&gt;
		// So JResponseJson was never called&lt;br /&gt;
		alert(&#039;Ajax error&#039;);&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onError: function(text, error)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request was answered by the server, but&lt;br /&gt;
		// the response was no valid JSON (this happens sometimes if there were PHP errors,&lt;br /&gt;
		// warnings or notices during the development process of a new Ajax request).&lt;br /&gt;
		alert(error + &amp;quot;\n\n&amp;quot; + text);&lt;br /&gt;
	}.bind(this)&lt;br /&gt;
});&lt;br /&gt;
req.post(&#039;anyparam=myvalue&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best Practice ==&lt;br /&gt;
As already seen above, code of the controller can be kept very simple using JResponseJson. The model is calculating the data which can be passed to JResponseJson afterwards (of course you can still modify it in the controller if you want to). If you are developing an MVC component you should save such a controller in a file called &#039;mycontroller.json.php&#039; and place it inside your &#039;controllers&#039; folder.&lt;br /&gt;
This way, that controller is automatically executed if your request URL contains &#039;format=json&#039;.&lt;br /&gt;
&lt;br /&gt;
Please note that as you can see in the example, there is no need for closing the application (e.g. $app-&amp;gt;close();) because the architecture of Joomla! handles that for you. So, this is very clean and good programming.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($count);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Additional Note===&lt;br /&gt;
&lt;br /&gt;
If you want to support no-js users by for example doing the same task with a normal request and a final redirect back to the page, there is not much you have do additionally. Just create another controller (now with name &#039;mycontroller.php&#039;) and code like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    $app = JFactory::getApplication();&lt;br /&gt;
    $app-&amp;gt;setRedirect(JRoute::_(&#039;index.php?option=com_mycomponent&amp;amp;view=myview&#039;, false));&lt;br /&gt;
&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::plural(&#039;COM_COMPONENT_COUNT_TASK&#039;, $count));&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::sprintf(&#039;COM_COMPONENT_COUNT_TASK_ERROR&#039;, $e-&amp;gt;getMessage()), &#039;error&#039;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No changes necessary in the model!&lt;br /&gt;
&lt;br /&gt;
So depending on whether you do the request with &#039;format=json&#039; (Ajax-Request) or via normal request (without &#039;format&#039; parameter), the correct controller is executed automatically and the response is prepared accordingly.&lt;br /&gt;
&lt;br /&gt;
[[Category:Extension development]]&lt;br /&gt;
[[Category:Component Development]]&lt;br /&gt;
[[Category:AJAX]]&lt;/div&gt;</summary>
		<author><name>Chraneco</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=104006</id>
		<title>JSON Responses with JResponseJson</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=104006"/>
		<updated>2013-09-28T20:26:01Z</updated>

		<summary type="html">&lt;p&gt;Chraneco: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|3.x}}&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Recently a new class JResponseJson was added to Joomla! 3.x which is able to simplify things with Ajax requests. With that class it is now possible to prepare responses to Ajax requests in a standardized and easy manner. You can have a look at the file here:&lt;br /&gt;
https://github.com/joomla/joomla-cms/blob/master/libraries/cms/response/json.php&lt;br /&gt;
It will mostly be used in controllers of components where you get the following advantages:&lt;br /&gt;
&lt;br /&gt;
* Using the flag &#039;success&#039; the JavaScript code (whether Mootools JRequest.JSON or jQuery.getJSON) can check whether the task was successful or not and react accordingly (If the request itself failes the error event of the JavaScript API can be used).&lt;br /&gt;
* Then, the actual response data (if any) can be retrieved from &#039;data&#039; in the JSON object and&lt;br /&gt;
* optionally a main response message from &#039;message&#039;.&lt;br /&gt;
* Additionally, all gathered messages in the JApplication message queue are also automatically sent back in &#039;messages&#039;. This can optionally be turned off.&lt;br /&gt;
&lt;br /&gt;
Another advantage is that no $app-&amp;gt;close(); is necessary anymore if the Ajax request is done with &#039;format=json&#039; because the already existing API handles the rest. Just echo the response object at the end of the task.&lt;br /&gt;
&lt;br /&gt;
== How to Use ==&lt;br /&gt;
=== Most Common Cases ===&lt;br /&gt;
&lt;br /&gt;
Here is an example controller file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $result = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;createSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($result);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see in the default case the return value of something that has been calculated by the model is simply pushed into a new JResponseJson object and written to the output. This will automatically create a JSON encoded string as follows (except that it is more compressed in reality):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:null,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in the &#039;data&#039; field you can send any array, object or value you want and the &#039;success&#039; flag is automatically set to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
If any exception occured in the model this exception is simply passed directly to a new JResponseJson object in our example which would create the following output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;This is the message of the exception&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:null}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it is an exception the &#039;success&#039; flag was automatically set to &#039;false&#039; and the message of the exception became the main response message.&lt;br /&gt;
&lt;br /&gt;
=== Additional Options ===&lt;br /&gt;
&lt;br /&gt;
If not an exception is passed as the first argument of the JResponseJson constructor you can specify an arbitrary main response message by passing a string as the second argument:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_SUCCESS&#039;));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;The request was successful.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can also manually set the error flag to &#039;false&#039; with the help of the third argument ($error):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_ERROR&#039;), true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;The was an error.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Please note that this way you can also send some data back.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Regardless of having an error response or a success response JResponseJson sends all messages back to the client that have been gathered in the application object:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$app = JFactory::getApplication();&lt;br /&gt;
// Some code ($result = ...)&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;This part was successful&#039;);&lt;br /&gt;
// Some more code&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;Here was a small warning&#039;);&lt;br /&gt;
&lt;br /&gt;
echo new JResponseJson($result, &#039;Main response message&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;Main response message&amp;quot;,&amp;quot;messages&amp;quot;:&amp;quot;&amp;lt;&amp;lt;all the encoded messages of $app&amp;gt;&amp;gt;&amp;quot;,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can see the big advantage of that below in the JavaScript section.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t want to send the messages back (rather that they stay in the session), simply set the fourth argument ($ignoreMessages) of the contructor to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Corresponding JavaScript Code ===&lt;br /&gt;
&lt;br /&gt;
Here is some sample JavaScript used which can be used on the client side together with JResponseJson on the server side.&lt;br /&gt;
The example is written with some Mootools code, but something similar can also be done with jQuery or any other JavaScript library for Ajax requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var req = new Request.JSON({&lt;br /&gt;
	method: &#039;post&#039;,&lt;br /&gt;
	url: &#039;index.php?option=com_component&amp;amp;task=mycontroller.execute&amp;amp;format=json&#039;,&lt;br /&gt;
	onSuccess: function(r)&lt;br /&gt;
	{&lt;br /&gt;
		if (!r.success &amp;amp;&amp;amp; r.message)&lt;br /&gt;
		{&lt;br /&gt;
			// Success flag is set to &#039;false&#039; and main response message given&lt;br /&gt;
			// So you can alert it or insert it into some HTML element&lt;br /&gt;
			alert(r.message);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.messages)&lt;br /&gt;
		{&lt;br /&gt;
			// All the enqueued messages of the $app object can simple be&lt;br /&gt;
			// rendered by the respective helper function of Joomla!&lt;br /&gt;
			// They will automatically be displayed at the messages section of the template&lt;br /&gt;
			Joomla.renderMessages(r.messages);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.data)&lt;br /&gt;
		{&lt;br /&gt;
			// Here you can access all the data of your response&lt;br /&gt;
			alert(r.data.myfirstcustomparam);&lt;br /&gt;
			alert(r.data.mysecondcustomparam);&lt;br /&gt;
		}&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onFailure: function(xhr)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request itself was not successful&lt;br /&gt;
		// So JResponseJson was never called&lt;br /&gt;
		alert(&#039;Ajax error&#039;);&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onError: function(text, error)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request was answered by the server, but&lt;br /&gt;
		// the response was no valid JSON (this happens sometimes if there were PHP errors,&lt;br /&gt;
		// warnings or notices during the development process of a new Ajax request).&lt;br /&gt;
		alert(error + &amp;quot;\n\n&amp;quot; + text);&lt;br /&gt;
	}.bind(this)&lt;br /&gt;
});&lt;br /&gt;
req.post(&#039;anyparam=myvalue&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best Practice ==&lt;br /&gt;
As already seen above, code of the controller can be kept very simple using JResponseJson. The model is calculating the data which can be passed to JResponseJson afterwards (of course you can still modify it in the controller if you want to). If you are developing an MVC component you should save such a controller in a file called &#039;mycontroller.json.php&#039; and place it inside your &#039;controllers&#039; folder.&lt;br /&gt;
This way, that controller is automatically executed if your request URL contains &#039;format=json&#039;.&lt;br /&gt;
&lt;br /&gt;
Please note that as you can see in the example, there is no need for closing the application (e.g. $app-&amp;gt;close();) because the architecture of Joomla! handles that for you. So, this is very clean and good programming.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($count);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Additional Note===&lt;br /&gt;
&lt;br /&gt;
If you want to support no-js users by for example doing the same task with a normal request and a final redirect back to the page, there is not much you have do additionally. Just create another controller (now with name &#039;mycontroller.php&#039;) and code like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    $app = JFactory::getApplication();&lt;br /&gt;
    $app-&amp;gt;setRedirect(JRoute::_(&#039;index.php?option=com_mycomponent&amp;amp;view=myview&#039;, false));&lt;br /&gt;
&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::plural(&#039;COM_COMPONENT_COUNT_TASK&#039;, $count));&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::sprintf(&#039;COM_COMPONENT_COUNT_TASK_ERROR&#039;, $e-&amp;gt;getMessage()), &#039;error&#039;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No changes necessary in the model!&lt;br /&gt;
&lt;br /&gt;
So depending on whether you do the request with &#039;format=json&#039; (Ajax-Request) or via normal request (without &#039;format&#039; parameter), the correct controller is executed automatically and the response is prepared accordingly.&lt;br /&gt;
&lt;br /&gt;
[[Category:Extension development]]&lt;br /&gt;
[[Category:Component Development]]&lt;br /&gt;
[[Category:AJAX]]&lt;/div&gt;</summary>
		<author><name>Chraneco</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=103994</id>
		<title>JSON Responses with JResponseJson</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=103994"/>
		<updated>2013-09-27T23:39:57Z</updated>

		<summary type="html">&lt;p&gt;Chraneco: /* Additional Note */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|3.x}}&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Recently a new class JResponseJson was added to Joomla! 3.x which is able to simplify things with Ajax requests. With that class it is now possible to prepare responses to Ajax requests in a standardized and easy manner. You can have a look at the file here:&lt;br /&gt;
https://github.com/joomla/joomla-cms/blob/master/libraries/cms/response/json.php&lt;br /&gt;
It will mostly be used in controllers of components where you get the following advantages:&lt;br /&gt;
&lt;br /&gt;
* Using the flag &#039;success&#039; the JavaScript code (whether Mootools JRequest.JSON or jQuery.getJSON) can check whether the task was successful or not and react accordingly (If the request itself failes the error event of the JavaScript API can be used).&lt;br /&gt;
* Then, the actual response data (if any) can be retrieved from data in the JSON object and&lt;br /&gt;
* optionally a main response message from message.&lt;br /&gt;
* Additionally, all gathered messages in the JApplication message queue are also automatically sent back in messages. This can optionally be turned off.&lt;br /&gt;
&lt;br /&gt;
Another advantage is that no $app-&amp;gt;close(); is necessary anymore if the Ajax request is done with &#039;format=json&#039; because the already existing API handles the rest. Just echo the response object at the end of the task.&lt;br /&gt;
&lt;br /&gt;
== How to Use ==&lt;br /&gt;
=== Most Common Cases ===&lt;br /&gt;
&lt;br /&gt;
Here is an example controller file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $result = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;createSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($result);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see in the default case the return value of something that has been calculated by the model is simply pushed into a new JResponseJson object and written to the output. This will automatically create a JSON encoded string as follows (except that it is more compressed in reality):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:null,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in the &#039;data&#039; field you can send any array, object or value you want and the &#039;success&#039; flag is automatically set to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
If any exception occured in the model this exception is simply passed directly to a new JResponseJson object in our example which would create the following output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;This is the message of the exception&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:null}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it is an exception the &#039;success&#039; flag was automatically set to &#039;false&#039; and the message of the exception became the main response message.&lt;br /&gt;
&lt;br /&gt;
=== Additional Options ===&lt;br /&gt;
&lt;br /&gt;
If not an exception is passed as the first argument of the JResponseJson constructor you can specify an arbitrary main response message by passing a string as the second argument:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_SUCCESS&#039;));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;The request was successful.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can also manually set the error flag to &#039;false&#039; with the help of the third argument ($error):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_ERROR&#039;), true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;The was an error.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Please note that this way you can also send some data back.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Regardless of having an error response or a success response JResponseJson sends all messages back to the client that have been gathered in the application object:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$app = JFactory::getApplication();&lt;br /&gt;
// Some code ($result = ...)&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;This part was successful&#039;);&lt;br /&gt;
// Some more code&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;Here was a small warning&#039;);&lt;br /&gt;
&lt;br /&gt;
echo new JResponseJson($result, &#039;Main response message&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;Main response message&amp;quot;,&amp;quot;messages&amp;quot;:&amp;quot;&amp;lt;&amp;lt;all the encoded messages of $app&amp;gt;&amp;gt;&amp;quot;,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can see the big advantage of that below in the JavaScript section.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t want to send the messages back (rather that they stay in the session), simply set the fourth argument ($ignoreMessages) of the contructor to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Corresponding JavaScript Code ===&lt;br /&gt;
&lt;br /&gt;
Here is some sample JavaScript used which can be used on the client side together with JResponseJson on the server side.&lt;br /&gt;
The example is written with some Mootools code, but something similar can also be done with jQuery or any other JavaScript library for Ajax requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var req = new Request.JSON({&lt;br /&gt;
	method: &#039;post&#039;,&lt;br /&gt;
	url: &#039;index.php?option=com_component&amp;amp;task=mycontroller.execute&amp;amp;format=json&#039;,&lt;br /&gt;
	onSuccess: function(r)&lt;br /&gt;
	{&lt;br /&gt;
		if (!r.success &amp;amp;&amp;amp; r.message)&lt;br /&gt;
		{&lt;br /&gt;
			// Success flag is set to &#039;false&#039; and main response message given&lt;br /&gt;
			// So you can alert it or insert it into some HTML element&lt;br /&gt;
			alert(r.message);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.messages)&lt;br /&gt;
		{&lt;br /&gt;
			// All the enqueued messages of the $app object can simple be&lt;br /&gt;
			// rendered by the respective helper function of Joomla!&lt;br /&gt;
			// They will automatically be displayed at the messages section of the template&lt;br /&gt;
			Joomla.renderMessages(r.messages);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.data)&lt;br /&gt;
		{&lt;br /&gt;
			// Here you can access all the data of your response&lt;br /&gt;
			alert(r.data.myfirstcustomparam);&lt;br /&gt;
			alert(r.data.mysecondcustomparam);&lt;br /&gt;
		}&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onFailure: function(xhr)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request itself was not successful&lt;br /&gt;
		// So JResponseJson was never called&lt;br /&gt;
		alert(&#039;Ajax error&#039;);&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onError: function(text, error)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request was answered by the server, but&lt;br /&gt;
		// the response was no valid JSON (this happens sometimes if there were PHP errors,&lt;br /&gt;
		// warnings or notices during the development process of a new Ajax request).&lt;br /&gt;
		alert(error + &amp;quot;\n\n&amp;quot; + text);&lt;br /&gt;
	}.bind(this)&lt;br /&gt;
});&lt;br /&gt;
req.post(&#039;anyparam=myvalue&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best Practice ==&lt;br /&gt;
As already seen above, code of the controller can be kept very simple using JResponseJson. The model is calculating the data which can be passed to JResponseJson afterwards (of course you can still modify it in the controller if you want to). If you are developing an MVC component you should save such a controller in a file called &#039;mycontroller.json.php&#039; and place it inside your &#039;controllers&#039; folder.&lt;br /&gt;
This way, that controller is automatically executed if your request URL contains &#039;format=json&#039;.&lt;br /&gt;
&lt;br /&gt;
Please note that as you can see in the example, there is no need for closing the application (e.g. $app-&amp;gt;close();) because the architecture of Joomla! handles that for you. So, this is very clean and good programming.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($count);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Additional Note===&lt;br /&gt;
&lt;br /&gt;
If you want to support no-js users by for example doing the same task with a normal request and a final redirect back to the page, there is not much you have do additionally. Just create another controller (now with name &#039;mycontroller.php&#039;) and code like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    $app = JFactory::getApplication();&lt;br /&gt;
    $app-&amp;gt;setRedirect(JRoute::_(&#039;index.php?option=com_mycomponent&amp;amp;view=myview&#039;, false));&lt;br /&gt;
&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::plural(&#039;COM_COMPONENT_COUNT_TASK&#039;, $count));&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::sprintf(&#039;COM_COMPONENT_COUNT_TASK_ERROR&#039;, $e-&amp;gt;getMessage()), &#039;error&#039;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No changes necessary in the model!&lt;br /&gt;
&lt;br /&gt;
So depending on whether you do the request with &#039;format=json&#039; (Ajax-Request) or via normal request (without &#039;format&#039; parameter), the correct controller is executed automatically and the response is prepared accordingly.&lt;/div&gt;</summary>
		<author><name>Chraneco</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=103993</id>
		<title>JSON Responses with JResponseJson</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=103993"/>
		<updated>2013-09-27T23:39:06Z</updated>

		<summary type="html">&lt;p&gt;Chraneco: /* Additional Note */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|3.x}}&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Recently a new class JResponseJson was added to Joomla! 3.x which is able to simplify things with Ajax requests. With that class it is now possible to prepare responses to Ajax requests in a standardized and easy manner. You can have a look at the file here:&lt;br /&gt;
https://github.com/joomla/joomla-cms/blob/master/libraries/cms/response/json.php&lt;br /&gt;
It will mostly be used in controllers of components where you get the following advantages:&lt;br /&gt;
&lt;br /&gt;
* Using the flag &#039;success&#039; the JavaScript code (whether Mootools JRequest.JSON or jQuery.getJSON) can check whether the task was successful or not and react accordingly (If the request itself failes the error event of the JavaScript API can be used).&lt;br /&gt;
* Then, the actual response data (if any) can be retrieved from data in the JSON object and&lt;br /&gt;
* optionally a main response message from message.&lt;br /&gt;
* Additionally, all gathered messages in the JApplication message queue are also automatically sent back in messages. This can optionally be turned off.&lt;br /&gt;
&lt;br /&gt;
Another advantage is that no $app-&amp;gt;close(); is necessary anymore if the Ajax request is done with &#039;format=json&#039; because the already existing API handles the rest. Just echo the response object at the end of the task.&lt;br /&gt;
&lt;br /&gt;
== How to Use ==&lt;br /&gt;
=== Most Common Cases ===&lt;br /&gt;
&lt;br /&gt;
Here is an example controller file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $result = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;createSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($result);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see in the default case the return value of something that has been calculated by the model is simply pushed into a new JResponseJson object and written to the output. This will automatically create a JSON encoded string as follows (except that it is more compressed in reality):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:null,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in the &#039;data&#039; field you can send any array, object or value you want and the &#039;success&#039; flag is automatically set to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
If any exception occured in the model this exception is simply passed directly to a new JResponseJson object in our example which would create the following output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;This is the message of the exception&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:null}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it is an exception the &#039;success&#039; flag was automatically set to &#039;false&#039; and the message of the exception became the main response message.&lt;br /&gt;
&lt;br /&gt;
=== Additional Options ===&lt;br /&gt;
&lt;br /&gt;
If not an exception is passed as the first argument of the JResponseJson constructor you can specify an arbitrary main response message by passing a string as the second argument:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_SUCCESS&#039;));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;The request was successful.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can also manually set the error flag to &#039;false&#039; with the help of the third argument ($error):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_ERROR&#039;), true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;The was an error.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Please note that this way you can also send some data back.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Regardless of having an error response or a success response JResponseJson sends all messages back to the client that have been gathered in the application object:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$app = JFactory::getApplication();&lt;br /&gt;
// Some code ($result = ...)&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;This part was successful&#039;);&lt;br /&gt;
// Some more code&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;Here was a small warning&#039;);&lt;br /&gt;
&lt;br /&gt;
echo new JResponseJson($result, &#039;Main response message&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;Main response message&amp;quot;,&amp;quot;messages&amp;quot;:&amp;quot;&amp;lt;&amp;lt;all the encoded messages of $app&amp;gt;&amp;gt;&amp;quot;,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can see the big advantage of that below in the JavaScript section.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t want to send the messages back (rather that they stay in the session), simply set the fourth argument ($ignoreMessages) of the contructor to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Corresponding JavaScript Code ===&lt;br /&gt;
&lt;br /&gt;
Here is some sample JavaScript used which can be used on the client side together with JResponseJson on the server side.&lt;br /&gt;
The example is written with some Mootools code, but something similar can also be done with jQuery or any other JavaScript library for Ajax requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var req = new Request.JSON({&lt;br /&gt;
	method: &#039;post&#039;,&lt;br /&gt;
	url: &#039;index.php?option=com_component&amp;amp;task=mycontroller.execute&amp;amp;format=json&#039;,&lt;br /&gt;
	onSuccess: function(r)&lt;br /&gt;
	{&lt;br /&gt;
		if (!r.success &amp;amp;&amp;amp; r.message)&lt;br /&gt;
		{&lt;br /&gt;
			// Success flag is set to &#039;false&#039; and main response message given&lt;br /&gt;
			// So you can alert it or insert it into some HTML element&lt;br /&gt;
			alert(r.message);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.messages)&lt;br /&gt;
		{&lt;br /&gt;
			// All the enqueued messages of the $app object can simple be&lt;br /&gt;
			// rendered by the respective helper function of Joomla!&lt;br /&gt;
			// They will automatically be displayed at the messages section of the template&lt;br /&gt;
			Joomla.renderMessages(r.messages);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.data)&lt;br /&gt;
		{&lt;br /&gt;
			// Here you can access all the data of your response&lt;br /&gt;
			alert(r.data.myfirstcustomparam);&lt;br /&gt;
			alert(r.data.mysecondcustomparam);&lt;br /&gt;
		}&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onFailure: function(xhr)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request itself was not successful&lt;br /&gt;
		// So JResponseJson was never called&lt;br /&gt;
		alert(&#039;Ajax error&#039;);&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onError: function(text, error)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request was answered by the server, but&lt;br /&gt;
		// the response was no valid JSON (this happens sometimes if there were PHP errors,&lt;br /&gt;
		// warnings or notices during the development process of a new Ajax request).&lt;br /&gt;
		alert(error + &amp;quot;\n\n&amp;quot; + text);&lt;br /&gt;
	}.bind(this)&lt;br /&gt;
});&lt;br /&gt;
req.post(&#039;anyparam=myvalue&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best Practice ==&lt;br /&gt;
As already seen above, code of the controller can be kept very simple using JResponseJson. The model is calculating the data which can be passed to JResponseJson afterwards (of course you can still modify it in the controller if you want to). If you are developing an MVC component you should save such a controller in a file called &#039;mycontroller.json.php&#039; and place it inside your &#039;controllers&#039; folder.&lt;br /&gt;
This way, that controller is automatically executed if your request URL contains &#039;format=json&#039;.&lt;br /&gt;
&lt;br /&gt;
Please note that as you can see in the example, there is no need for closing the application (e.g. $app-&amp;gt;close();) because the architecture of Joomla! handles that for you. So, this is very clean and good programming.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($count);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Additional Note===&lt;br /&gt;
&lt;br /&gt;
If you want to support no-js users by for example doing the same task with a normal request and a final redirect back to the page, there is not much you have do additionally. Just create another controller (now with name &#039;mycontroller.php&#039;) and code like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    $app = JFactory::getApplication();&lt;br /&gt;
    $app-&amp;gt;setRedirect(JRoute::_(&#039;index.php?option=com_mycomponent&amp;amp;view=myview&#039;, false));&lt;br /&gt;
&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::plural(&#039;COM_COMPONENT_COUNT_TASK&#039;, $count));&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::sprintf(&#039;COM_COMPONENT_COUNT_TASK_ERROR&#039;, $e-&amp;gt;getMessage()), &#039;error&#039;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No changes necessary in the model!&lt;br /&gt;
&lt;br /&gt;
So depending on whether you do the request with &#039;format=json&#039; (Ajax-Request) or view normal request (without &#039;format&#039; parameter) the correct controller is executed automatically and the response prepared accordingly.&lt;/div&gt;</summary>
		<author><name>Chraneco</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=103992</id>
		<title>JSON Responses with JResponseJson</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=103992"/>
		<updated>2013-09-27T23:32:04Z</updated>

		<summary type="html">&lt;p&gt;Chraneco: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|3.x}}&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Recently a new class JResponseJson was added to Joomla! 3.x which is able to simplify things with Ajax requests. With that class it is now possible to prepare responses to Ajax requests in a standardized and easy manner. You can have a look at the file here:&lt;br /&gt;
https://github.com/joomla/joomla-cms/blob/master/libraries/cms/response/json.php&lt;br /&gt;
It will mostly be used in controllers of components where you get the following advantages:&lt;br /&gt;
&lt;br /&gt;
* Using the flag &#039;success&#039; the JavaScript code (whether Mootools JRequest.JSON or jQuery.getJSON) can check whether the task was successful or not and react accordingly (If the request itself failes the error event of the JavaScript API can be used).&lt;br /&gt;
* Then, the actual response data (if any) can be retrieved from data in the JSON object and&lt;br /&gt;
* optionally a main response message from message.&lt;br /&gt;
* Additionally, all gathered messages in the JApplication message queue are also automatically sent back in messages. This can optionally be turned off.&lt;br /&gt;
&lt;br /&gt;
Another advantage is that no $app-&amp;gt;close(); is necessary anymore if the Ajax request is done with &#039;format=json&#039; because the already existing API handles the rest. Just echo the response object at the end of the task.&lt;br /&gt;
&lt;br /&gt;
== How to Use ==&lt;br /&gt;
=== Most Common Cases ===&lt;br /&gt;
&lt;br /&gt;
Here is an example controller file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $result = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;createSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($result);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see in the default case the return value of something that has been calculated by the model is simply pushed into a new JResponseJson object and written to the output. This will automatically create a JSON encoded string as follows (except that it is more compressed in reality):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:null,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in the &#039;data&#039; field you can send any array, object or value you want and the &#039;success&#039; flag is automatically set to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
If any exception occured in the model this exception is simply passed directly to a new JResponseJson object in our example which would create the following output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;This is the message of the exception&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:null}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it is an exception the &#039;success&#039; flag was automatically set to &#039;false&#039; and the message of the exception became the main response message.&lt;br /&gt;
&lt;br /&gt;
=== Additional Options ===&lt;br /&gt;
&lt;br /&gt;
If not an exception is passed as the first argument of the JResponseJson constructor you can specify an arbitrary main response message by passing a string as the second argument:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_SUCCESS&#039;));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;The request was successful.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can also manually set the error flag to &#039;false&#039; with the help of the third argument ($error):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_ERROR&#039;), true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;The was an error.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Please note that this way you can also send some data back.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Regardless of having an error response or a success response JResponseJson sends all messages back to the client that have been gathered in the application object:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$app = JFactory::getApplication();&lt;br /&gt;
// Some code ($result = ...)&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;This part was successful&#039;);&lt;br /&gt;
// Some more code&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;Here was a small warning&#039;);&lt;br /&gt;
&lt;br /&gt;
echo new JResponseJson($result, &#039;Main response message&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;Main response message&amp;quot;,&amp;quot;messages&amp;quot;:&amp;quot;&amp;lt;&amp;lt;all the encoded messages of $app&amp;gt;&amp;gt;&amp;quot;,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can see the big advantage of that below in the JavaScript section.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t want to send the messages back (rather that they stay in the session), simply set the fourth argument ($ignoreMessages) of the contructor to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Corresponding JavaScript Code ===&lt;br /&gt;
&lt;br /&gt;
Here is some sample JavaScript used which can be used on the client side together with JResponseJson on the server side.&lt;br /&gt;
The example is written with some Mootools code, but something similar can also be done with jQuery or any other JavaScript library for Ajax requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var req = new Request.JSON({&lt;br /&gt;
	method: &#039;post&#039;,&lt;br /&gt;
	url: &#039;index.php?option=com_component&amp;amp;task=mycontroller.execute&amp;amp;format=json&#039;,&lt;br /&gt;
	onSuccess: function(r)&lt;br /&gt;
	{&lt;br /&gt;
		if (!r.success &amp;amp;&amp;amp; r.message)&lt;br /&gt;
		{&lt;br /&gt;
			// Success flag is set to &#039;false&#039; and main response message given&lt;br /&gt;
			// So you can alert it or insert it into some HTML element&lt;br /&gt;
			alert(r.message);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.messages)&lt;br /&gt;
		{&lt;br /&gt;
			// All the enqueued messages of the $app object can simple be&lt;br /&gt;
			// rendered by the respective helper function of Joomla!&lt;br /&gt;
			// They will automatically be displayed at the messages section of the template&lt;br /&gt;
			Joomla.renderMessages(r.messages);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.data)&lt;br /&gt;
		{&lt;br /&gt;
			// Here you can access all the data of your response&lt;br /&gt;
			alert(r.data.myfirstcustomparam);&lt;br /&gt;
			alert(r.data.mysecondcustomparam);&lt;br /&gt;
		}&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onFailure: function(xhr)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request itself was not successful&lt;br /&gt;
		// So JResponseJson was never called&lt;br /&gt;
		alert(&#039;Ajax error&#039;);&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onError: function(text, error)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request was answered by the server, but&lt;br /&gt;
		// the response was no valid JSON (this happens sometimes if there were PHP errors,&lt;br /&gt;
		// warnings or notices during the development process of a new Ajax request).&lt;br /&gt;
		alert(error + &amp;quot;\n\n&amp;quot; + text);&lt;br /&gt;
	}.bind(this)&lt;br /&gt;
});&lt;br /&gt;
req.post(&#039;anyparam=myvalue&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best Practice ==&lt;br /&gt;
As already seen above, code of the controller can be kept very simple using JResponseJson. The model is calculating the data which can be passed to JResponseJson afterwards (of course you can still modify it in the controller if you want to). If you are developing an MVC component you should save such a controller in a file called &#039;mycontroller.json.php&#039; and place it inside your &#039;controllers&#039; folder.&lt;br /&gt;
This way, that controller is automatically executed if your request URL contains &#039;format=json&#039;.&lt;br /&gt;
&lt;br /&gt;
Please note that as you can see in the example, there is no need for closing the application (e.g. $app-&amp;gt;close();) because the architecture of Joomla! handles that for you. So, this is very clean and good programming.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($count);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Additional Note===&lt;br /&gt;
&lt;br /&gt;
If you want to support no-js users by for example doing the same task with a normal request and a final redirect back to the page, there is not much you have do additionally. Just create another controller (now with name &#039;mycontroller.php&#039;) and code like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    $app = JFactory::getApplication();&lt;br /&gt;
    $app-&amp;gt;setRedirect(JRoute::_(&#039;index.php?option=com_mycomponent&amp;amp;view=myview&#039;, false));&lt;br /&gt;
&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::plural(&#039;COM_COMPONENT_COUNT_TASK&#039;, $count));&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::sprintf(&#039;COM_COMPONENT_COUNT_TASK_ERROR&#039;, $e-&amp;gt;getMessage()));&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No changes necessary in the model!&lt;br /&gt;
&lt;br /&gt;
So depending on whether you do the request with &#039;format=json&#039; (Ajax-Request) or view normal request (without &#039;format&#039; parameter) the correct controller is executed automatically and the response prepared accordingly.&lt;/div&gt;</summary>
		<author><name>Chraneco</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Portal:Developers/Miscellaneous&amp;diff=103991</id>
		<title>Portal:Developers/Miscellaneous</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Portal:Developers/Miscellaneous&amp;diff=103991"/>
		<updated>2013-09-27T23:30:28Z</updated>

		<summary type="html">&lt;p&gt;Chraneco: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* [[Retrieving request data using JInput]] {{JVer|2.5}} and newer&lt;br /&gt;
* [[Retrieving data from GET and POST requests]] {{JVer|1.5}}&lt;br /&gt;
* [[How to use the filesystem package]]&lt;br /&gt;
* [[Application execution order]]&lt;br /&gt;
* [[Accessing the current user object]]&lt;br /&gt;
* [[Adding JavaScript and CSS to the page]]&lt;br /&gt;
* [[Constants]] used in the Joomla [[Framework]].&lt;br /&gt;
* [[How to create a custom button]]&lt;br /&gt;
* [[How to create a stand-alone application using the Joomla! Framework]]&lt;br /&gt;
* [[How to use user state variables]]&lt;br /&gt;
* [[Cache]] and [[Using caching to speed up your code]]&lt;br /&gt;
* [[Using the installer API to support package installation]]&lt;br /&gt;
* [[How to add tooltips to your Joomla! website]]&lt;br /&gt;
* [[Display error messages and notices]].&lt;br /&gt;
* [[Sharing layouts across views or extensions with JLayout]] {{JVer|3.1}}&lt;br /&gt;
* [[Using JLog]]&lt;br /&gt;
* [[JSON Responses with JResponseJson]] {{JVer|3.x}}&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Landing subpages|{{PAGENAME}}]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Chraneco</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Portal:Component_Development/Reading_list&amp;diff=103990</id>
		<title>Portal:Component Development/Reading list</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Portal:Component_Development/Reading_list&amp;diff=103990"/>
		<updated>2013-09-27T23:28:35Z</updated>

		<summary type="html">&lt;p&gt;Chraneco: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* [[Developing a MVC Component|Developing a Model-View-Controller Component]] {{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[Adding ACL rules to your component]] {{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[Adding sortable columns to a table in a component]] {{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[Component Program Flow]].  {{JVer/multi|2.5,3.x}}&lt;br /&gt;
::&amp;lt;small&amp;gt;(UML sequence diagrams showing the control flow for a component.)&amp;lt;/small&amp;gt;&lt;br /&gt;
* [[Creating a file uploader in your component]] {{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[Creating a toolbar for your component]] {{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[J2.5:Creating Mootools accordion or simple slider/toggler|Creating Mootools accordion or simple slider/toggler]] {{JVer|2.5}}&lt;br /&gt;
* [[J2.5:Creating PDF views|Creating PDF views]] {{JVer|2.5}}&lt;br /&gt;
* [[File Structure and Naming Conventions]] {{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[How to add breadcrumbs]]{{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[How to add custom filters to components]] {{JVer|2.5}}&lt;br /&gt;
* [[How to implement XML-RPC in a component]]&lt;br /&gt;
* [[JController and its subclass usage overview]] {{JVer/multi|2.5}}&lt;br /&gt;
* [[Portal:Platform|Joomla Platform Portal]] {{JVer|platform}}&lt;br /&gt;
* [[J2.5:Managing Component Updates|Managing Component Updates - Part 1]] {{JVer|2.5}}&lt;br /&gt;
* [[Manifest files]] {{JVer/multi|2.5,3.x}}&lt;br /&gt;
::&amp;lt;small&amp;gt;(For the installation of extensions.)&amp;lt;/small&amp;gt;&lt;br /&gt;
* [[Sending email from extensions]]{{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[Supporting SEF URLs in your component]]{{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[Using a custom image in the menu bar title]]&lt;br /&gt;
* [[Using multiple models in an MVC component]] {{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[Using the JHtmlTabs class in a component]] {{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[Using the JToolBar class in the frontend]] {{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[J3.1:Using Tags in an Extension|Using Tags in an Extension]] {{JVer|3.x}}&lt;br /&gt;
* [[Xml-rpc changes in Joomla! 2.5]] {{JVer/multi|2.5,3.x}}&lt;br /&gt;
* [[JSON Responses with JResponseJson]] {{JVer|3.x}}&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Landing subpages|{{PAGENAME}}]]{{NOINDEX}}&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Chraneco</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=103989</id>
		<title>JSON Responses with JResponseJson</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=JSON_Responses_with_JResponseJson&amp;diff=103989"/>
		<updated>2013-09-27T23:26:55Z</updated>

		<summary type="html">&lt;p&gt;Chraneco: Initial Article&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|3.x}}&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Recently a new class JResponseJson was added to Joomla! 3.x which is able to simplify things with Ajax requests. With that class it is now possible to prepare responses to Ajax requests in a standardized and easy manner. You can have a look at the file here:&lt;br /&gt;
&lt;br /&gt;
It will mostly be used in controllers of components where you get the following advantages:&lt;br /&gt;
&lt;br /&gt;
* Using the flag &#039;success&#039; the JavaScript code (whether Mootools JRequest.JSON or jQuery.getJSON) can check whether the task was successful or not and react accordingly (If the request itself failes the error event of the JavaScript API can be used).&lt;br /&gt;
* Then, the actual response data (if any) can be retrieved from data in the JSON object and&lt;br /&gt;
* optionally a main response message from message.&lt;br /&gt;
* Additionally, all gathered messages in the JApplication message queue are also automatically sent back in messages. This can optionally be turned off.&lt;br /&gt;
&lt;br /&gt;
Another advantage is that no $app-&amp;gt;close(); is necessary anymore if the Ajax request is done with &#039;format=json&#039; because the already existing API handles the rest. Just echo the response object at the end of the task.&lt;br /&gt;
&lt;br /&gt;
== How to Use ==&lt;br /&gt;
=== Most Common Cases ===&lt;br /&gt;
&lt;br /&gt;
Here is an example controller file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $result = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;createSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($result);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see in the default case the return value of something that has been calculated by the model is simply pushed into a new JResponseJson object and written to the output. This will automatically create a JSON encoded string as follows (except that it is more compressed in reality):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:null,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in the &#039;data&#039; field you can send any array, object or value you want and the &#039;success&#039; flag is automatically set to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
If any exception occured in the model this exception is simply passed directly to a new JResponseJson object in our example which would create the following output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;This is the message of the exception&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:null}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it is an exception the &#039;success&#039; flag was automatically set to &#039;false&#039; and the message of the exception became the main response message.&lt;br /&gt;
&lt;br /&gt;
=== Additional Options ===&lt;br /&gt;
&lt;br /&gt;
If not an exception is passed as the first argument of the JResponseJson constructor you can specify an arbitrary main response message by passing a string as the second argument:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_SUCCESS&#039;));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;The request was successful.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can also manually set the error flag to &#039;false&#039; with the help of the third argument ($error):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo new JResponseJson($result, JText::_(&#039;COM_COMPONENT_MY_TASK_ERROR&#039;), true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which creates e.g.:&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:false,&amp;quot;message&amp;quot;:&amp;quot;The was an error.&amp;quot;,&amp;quot;messages&amp;quot;:null,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Please note that this way you can also send some data back.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Regardless of having an error response or a success response JResponseJson sends all messages back to the client that have been gathered in the application object:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$app = JFactory::getApplication();&lt;br /&gt;
// Some code ($result = ...)&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;This part was successful&#039;);&lt;br /&gt;
// Some more code&lt;br /&gt;
$app-&amp;gt;enqueueMessage(&#039;Here was a small warning&#039;);&lt;br /&gt;
&lt;br /&gt;
echo new JResponseJson($result, &#039;Main response message&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
{&amp;quot;success&amp;quot;:true,&amp;quot;message&amp;quot;:&amp;quot;Main response message&amp;quot;,&amp;quot;messages&amp;quot;:&amp;quot;&amp;lt;&amp;lt;all the encoded messages of $app&amp;gt;&amp;gt;&amp;quot;,&amp;quot;data&amp;quot;:{&amp;quot;myfirstcustomparam&amp;quot;:1,&amp;quot;mysecondcustomparam&amp;quot;:42, ...}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can see the big advantage of that below in the JavaScript section.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t want to send the messages back (rather that they stay in the session), simply set the fourth argument ($ignoreMessages) of the contructor to &#039;true&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Corresponding JavaScript Code ===&lt;br /&gt;
&lt;br /&gt;
Here is some sample JavaScript used which can be used on the client side together with JResponseJson on the server side.&lt;br /&gt;
The example is written with some Mootools code, but something similar can also be done with jQuery or any other JavaScript library for Ajax requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var req = new Request.JSON({&lt;br /&gt;
	method: &#039;post&#039;,&lt;br /&gt;
	url: &#039;index.php?option=com_component&amp;amp;task=mycontroller.execute&amp;amp;format=json&#039;,&lt;br /&gt;
	onSuccess: function(r)&lt;br /&gt;
	{&lt;br /&gt;
		if (!r.success &amp;amp;&amp;amp; r.message)&lt;br /&gt;
		{&lt;br /&gt;
			// Success flag is set to &#039;false&#039; and main response message given&lt;br /&gt;
			// So you can alert it or insert it into some HTML element&lt;br /&gt;
			alert(r.message);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.messages)&lt;br /&gt;
		{&lt;br /&gt;
			// All the enqueued messages of the $app object can simple be&lt;br /&gt;
			// rendered by the respective helper function of Joomla!&lt;br /&gt;
			// They will automatically be displayed at the messages section of the template&lt;br /&gt;
			Joomla.renderMessages(r.messages);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (r.data)&lt;br /&gt;
		{&lt;br /&gt;
			// Here you can access all the data of your response&lt;br /&gt;
			alert(r.data.myfirstcustomparam);&lt;br /&gt;
			alert(r.data.mysecondcustomparam);&lt;br /&gt;
		}&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onFailure: function(xhr)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request itself was not successful&lt;br /&gt;
		// So JResponseJson was never called&lt;br /&gt;
		alert(&#039;Ajax error&#039;);&lt;br /&gt;
	}.bind(this),&lt;br /&gt;
	onError: function(text, error)&lt;br /&gt;
	{&lt;br /&gt;
		// Reaching this point means that the Ajax request was answered by the server, but&lt;br /&gt;
		// the response was no valid JSON (this happens sometimes if there were PHP errors,&lt;br /&gt;
		// warnings or notices during the development process of a new Ajax request).&lt;br /&gt;
		alert(error + &amp;quot;\n\n&amp;quot; + text);&lt;br /&gt;
	}.bind(this)&lt;br /&gt;
});&lt;br /&gt;
req.post(&#039;anyparam=myvalue&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best Practice ==&lt;br /&gt;
As already seen above, code of the controller can be kept very simple using JResponseJson. The model is calculating the data which can be passed to JResponseJson afterwards (of course you can still modify it in the controller if you want to). If you are developing an MVC component you should save such a controller in a file called &#039;mycontroller.json.php&#039; and place it inside your &#039;controllers&#039; folder.&lt;br /&gt;
This way, that controller is automatically executed if your request URL contains &#039;format=json&#039;.&lt;br /&gt;
&lt;br /&gt;
Please note that as you can see in the example, there is no need for closing the application (e.g. $app-&amp;gt;close();) because the architecture of Joomla! handles that for you. So, this is very clean and good programming.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      echo new JResponseJson($count);&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      echo new JResponseJson($e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Additional Note===&lt;br /&gt;
&lt;br /&gt;
If you want to support no-js users by for example doing the same task with a normal request and a final redirect back to the page, there is not much you have do additionally. Just create another controller (now with name &#039;mycontroller.php&#039;) and code like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class MyController extends JControllerLegacy&lt;br /&gt;
{&lt;br /&gt;
  public function execute()&lt;br /&gt;
  {&lt;br /&gt;
    $app = JFactory::getApplication();&lt;br /&gt;
    $app-&amp;gt;setRedirect(JRoute::_(&#039;index.php?option=com_mycomponent&amp;amp;view=myview&#039;, false));&lt;br /&gt;
&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      $anyParam = JFactory::getApplication()-&amp;gt;input-&amp;gt;get(&#039;anyparam&#039;);&lt;br /&gt;
&lt;br /&gt;
      $count = $this-&amp;gt;getModel(&#039;example&#039;)-&amp;gt;countSomething($anyParam);&lt;br /&gt;
&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::plural(&#039;COM_COMPONENT_COUNT_TASK&#039;, $count));&lt;br /&gt;
    }&lt;br /&gt;
    catch(Exception $e)&lt;br /&gt;
    {&lt;br /&gt;
      $app-&amp;gt;setMessage(JText::sprintf(&#039;COM_COMPONENT_COUNT_TASK_ERROR&#039;, $e-&amp;gt;getMessage()));&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No changes necessary in the model!&lt;br /&gt;
&lt;br /&gt;
So depending on whether you do the request with &#039;format=json&#039; (Ajax-Request) or view normal request (without &#039;format&#039; parameter) the correct controller is executed automatically and the response prepared accordingly.&lt;/div&gt;</summary>
		<author><name>Chraneco</name></author>
	</entry>
</feed>