Joomla raiseError with AJAX get message

707 views
Skip to first unread message

Evandromar Machado

unread,
Mar 21, 2013, 10:21:56 AM3/21/13
to joomla-de...@googlegroups.com
Hello, I want to look very possible errors of a query to the database when using ajax calls.
When error happens, joomla and go error.php RaiseError calls and everything, I can not return an error response in json format.
Does anyone have example code using ajax and treating errors?
Thanks

Allon Moritz

unread,
Mar 21, 2013, 3:49:55 PM3/21/13
to joomla-de...@googlegroups.com
I'm using this in my controller
public static function sendMessage($message, $error = false, array $data = array()) {
ob_clean();

JLoader::import('components.com_languages.helpers.jsonresponse', JPATH_ADMINISTRATOR);
if (!$error) {
JFactory::getApplication()->enqueueMessage($message);
echo new JJsonResponse($data);
} else {
JFactory::getApplication()->enqueueMessage($message, 'error');
echo new JJsonResponse($data);
}

JFactory::getApplication()->close();
}

And in the js file something like
jQuery('#loader').show();
jQuery.ajax({
type: 'POST',
url: 'index.php?option=com_demo&task=event.move',
data: {id: event.id},
success: function (data) {
jQuery('#loader').hide();
var json = jQuery.parseJSON(data);
// do some stuff
Joomla.renderMessages(json.messages);
}
});

Hope that helps....



--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Dmitry Rekun

unread,
Mar 22, 2013, 1:59:25 AM3/22/13
to joomla-de...@googlegroups.com
That's interesting laoneo. What is the purpose of closing an application?

Dmitry

Allon Moritz

unread,
Mar 22, 2013, 5:58:56 AM3/22/13
to joomla-de...@googlegroups.com

It is a gracefull die...

On 22 Mar 2013 06:59, "Dmitry Rekun" <bzz...@gmail.com> wrote:
That's interesting laoneo. What is the purpose of closing an application?

Dmitry

piotr_cz

unread,
Mar 22, 2013, 6:12:42 AM3/22/13
to Joomla! General Development
There is/was a Pull request to move JResponseJson to it's own package
within the platform, I think it could be quite beneficiary to CMS:
https://github.com/joomla/joomla-platform/pull/1596

Do you think we can revive it?

Allon Moritz

unread,
Mar 22, 2013, 7:12:46 AM3/22/13
to joomla-de...@googlegroups.com
Before I was using the class from the language package I searched the whole cms up and down to have a library class like this one, but there wasn't one so I've used the one from the language package. I'm pretty sure this pull request would be a huge benefit for the extension developers as the people request more and more from my extensions not to have page loads but instead ajax calls.

piotr_cz

unread,
Mar 22, 2013, 7:35:16 AM3/22/13
to Joomla! General Development
I Agree, there should be a standard and solid foundation for creating
ajax/json responses.
Let's try to get it merged sake of all developers.

- Do you think this should be part of Framework o CMS?
- Do you want to add/ change something in package?

Maybe we should create explicit post like 'feedback for JJsonResponse
package' to get feedback from others, but IMHO this is ok for general
use.

Allon Moritz

unread,
Mar 22, 2013, 7:54:48 AM3/22/13
to joomla-de...@googlegroups.com
You will have my vote!

Michael Babker

unread,
Mar 22, 2013, 9:15:51 AM3/22/13
to joomla-de...@googlegroups.com
I'd +1 it if we can standardize its use in core.  Right now, there's a JSON response class in the admin com_finder (used with the indexer) and the install app (since that's using AJAX for all requests).  So, abstract out the response class and make it extendable for special use cases, and I think we have a win-win situation.

David Hurley

unread,
Mar 22, 2013, 9:21:28 AM3/22/13
to joomla-de...@googlegroups.com
I agree with Michael. If we can standardize an approach and take the processes currently in place but abstract them out for use elsewhere that would be an ideal situation.

-
Thanks,
David Hurley

Chraneco

unread,
Mar 22, 2013, 10:27:42 AM3/22/13
to joomla-de...@googlegroups.com
Hi,

when I created that pull request to the platform
https://github.com/joomla/joomla-platform/pull/1596
it was not accepted and only supported by very few people, but it seems that there are more supporters now.
So, I could redo the patch for the CMS, if it is accepted this time.

One other thing has to be fixed together with this. I created a post here for that:
https://groups.google.com/forum/#!topic/joomlabugsquad/YhxiAL5iaf8
and a tracker issue here:
http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=29425
but it seems that nobody is interested in fixing this.

If it would be fixed, sending Ajax responses could be done in a very clean and beautiful way with *.json.php controllers and without having to use ugly things like $app->close();

Regards
Chraneco

Evandromar Machado

unread,
Mar 22, 2013, 11:34:33 AM3/22/13
to joomla-de...@googlegroups.com
I got so many calls in my ajax component that would be difficult to count them.
I can get answers json perfectly, but if something's wrong as error database for example, the application "dies" and I get the error of no return!
It would be great to have that back.
I believe that by adding that the platform would be very good.

piotr_cz

unread,
Mar 22, 2013, 1:31:46 PM3/22/13
to Joomla! General Development
Using JJResponseJson, you'd do:

try
{
// Here exceptions might be thrown
$items = $db->query($query);
}
catch (Exception $e)
{
JFactory::getApplication()->close( new JResponseJson($e) );
// does { success: false, message: "database crashed "}
}

// Process $items from database
$data = $items;

JFactory::getApplication()->close( new JResponseJson($data) );
// does { success: true, data: [ { id: 1, title: "Item 1"}, { id: 2,
title: "Item 2" }] }

It's actually simplified process of FinderControllerIndexer
https://github.com/joomla/joomla-cms/blob/master/administrator/components/com_finder/controllers/indexer.json.php


On Mar 22, 4:34 pm, Evandromar Machado <evandromar.mach...@gmail.com>
wrote:
> I got so many calls in my ajax component that would be difficult to count
> them.
> I can get answers json perfectly, but if something's wrong as error
> database for example, the application "dies" and I get the error of no
> return!
> It would be great to have that back.
> I believe that by adding that the platform would be very good.
>
>
>
>
>
>
>
> On Friday, 22 March 2013 11:27:42 UTC-3, Chraneco wrote:
>
> > Hi,
>
> > when I created that pull request to the platform
> >https://github.com/joomla/joomla-platform/pull/1596
> > it was not accepted and only supported by very few people, but it seems
> > that there are more supporters now.
> > So, I could redo the patch for the CMS, if it is accepted this time.
>
> > One other thing has to be fixed together with this. I created a post here
> > for that:
> >https://groups.google.com/forum/#!topic/joomlabugsquad/YhxiAL5iaf8
> > and a tracker issue here:
>
> >http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEd...

piotr_cz

unread,
Mar 22, 2013, 1:52:06 PM3/22/13
to Joomla! General Development
Okay let's do it.
this Pull Request fulfills all my needs.

Only one thing I'm not completely sure about is whether to include
json outputing (response status, cache) or leave if like this.

Standard would be really helpful in that Frontend client could easily
communicate with various extensions/ core packages.

/5 minutes later

before we rush in, there's Joomla CMS Web Services API Specification:
https://docs.google.com/document/d/1FVKGlV6BN6pu-YH2WR2pQHE3Ez7M6r7LD417GSw9ZSo/edit#

I'm not sure about JResponseJson use with for this specification:
client-side framework might have problems with reading response with
error status codes (400-500). This is doable for MooTools but I know
little about jQuery


On Mar 22, 2:21 pm, David Hurley <david.hur...@joomla.org> wrote:
> I agree with Michael. If we can standardize an approach and take the
> processes currently in place but abstract them out for use elsewhere that
> would be an ideal situation.
>
> -
> Thanks,
> David Hurley
>
> On Fri, Mar 22, 2013 at 9:15 AM, Michael Babker <michael.bab...@gmail.com>wrote:
>
>
>
>
>
>
>
> > I'd +1 it if we can standardize its use in core.  Right now, there's a
> > JSON response class in the admin com_finder (used with the indexer) and the
> > install app (since that's using AJAX for all requests).  So, abstract out
> > the response class and make it extendable for special use cases, and I
> > think we have a win-win situation.
>
> > From: Allon Moritz <allon.mor...@gmail.com>
> > Reply-To: <joomla-de...@googlegroups.com>
> > Date: Friday, March 22, 2013 6:54 AM
> > To: <joomla-de...@googlegroups.com>
> > Subject: Re: [jgen] Re: Joomla raiseError with AJAX get message
>
> > You will have my vote!
>

Aymeric Dourthe

unread,
Mar 22, 2013, 1:58:00 PM3/22/13
to joomla-de...@googlegroups.com
Hi,

i follow this thread because this feature fit my need too.
I confirm that jQuery can deal without problem with error status codes.
There is an error handler with all information provided in.

Best regards,
Aymeric Dourthe
French web engineer

Le 22/03/2013 18:52, piotr_cz a �crit :

Alonzo Turner

unread,
Mar 22, 2013, 2:47:51 PM3/22/13
to joomla-de...@googlegroups.com
Perhaps, I misunderstand, but I believed there is already a standard way of doing this. When I create an AJAX request, I add the parameter "format=json". Then, the data returned is in JSON format because JDocumentJSON already handles all of the MIME type headers and closes the application for me. If I encounter an error while retrieving data in the model, I can pass that error message back to JavaScript by adding the error information to my JSON output.

I would also add an onFailure handler to my AJAX request, just as I have an onSuccess handler. Here is an example:

jQuery.ajax({
  type: 'POST',
  url: 'index.php',
  data: { option: 'com_myextension', view: 'myview', format: 'json' },
  success: function(data)
  {
      if(data.error_number){
          // DISPLAY ERROR MESSAGE
          return false;
      }
      // DO SOMETHING WITH RETURNED DATA
  },
  failure: function(data)
  {
      // HTTP RESPONSE RECEIVED IS NOT 200
  }
});

Additionally, shouldn't much of this get addressed with the RESTFUL Web Services API work that is being done in the platform to respond to data requests? That whole API project is based around the idea of communicating with joomla using JSON. I don't want to get behind any more changes that only last for one version. I don't want to see this pull request implemented if the Web Services groups is just going to turn around and undo it in 6 months.

Evandromar Machado

unread,
Mar 22, 2013, 3:22:40 PM3/22/13
to joomla-de...@googlegroups.com
Hello, forgive me if I'm doing it wrong!!
I managed to capture responses when exceptions occur, could make it work perfectly.
Joomla.renderMessages (data.messages) had no effect why?
The error messages were instanceof Exception, I changed the code below and now everything works perfectly, the co code below.
Thanks for the help and all.

public static function sendMessage($message, $error = false, array $data = array())
{
ob_clean();
if (!$error)
{
JFactory::getApplication()->enqueueMessage($message);
echo new JJsonResponse($data);
}
else
{
JFactory::getApplication()->enqueueMessage($message, 'error');
$response = new JJsonResponse($data, $message, true);
foreach ($response->messages['error'] as $i => $message){
if ($message instanceof Exception)
{
$response->messages['error'][$i] = $message->getMessage();
}
}
echo $response;
}
JFactory::getApplication()->close();
}

In script ...

success: function (data){
if (data) {
if (!data.success) {
Joomla.renderMessages(data.messages);
return false;
}
}
}
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

piotr_cz

unread,
Mar 27, 2013, 6:39:19 AM3/27/13
to Joomla! General Development
This is interesting:

j3-rest-api
https://github.com/chrisdavenport/j3-rest-api

Web services proof-of-concept code
https://groups.google.com/group/joomla-dev-cms/browse_frm/thread/986e7a96900e03ce#



On Mar 22, 3:27 pm, Chraneco <chran...@googlemail.com> wrote:
> Hi,
>
> when I created that pull request to the platformhttps://github.com/joomla/joomla-platform/pull/1596
> it was not accepted and only supported by very few people, but it seems
> that there are more supporters now.
> So, I could redo the patch for the CMS, if it is accepted this time.
>
> One other thing has to be fixed together with this. I created a post here
> for that:https://groups.google.com/forum/#!topic/joomlabugsquad/YhxiAL5iaf8
> and a tracker issue here:http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEd...
Message has been deleted

Youjoomla.com

unread,
Mar 31, 2013, 11:50:51 AM3/31/13
to joomla-de...@googlegroups.com
I was on this issue for couple of days 
Download our template framework base template YouGrids  v1.0.14

go to elements folder and get yjsgjson.php  ,  it is custom 50 lines JSON class from Contsantin, 
please keep the copyrights in tact 

now open 

elements/yjsgupdate.php

on line 49 you will see 
require('yjsgjson.php');
yo will need that in your ajax file 

from line 160 - 185  is where the errors are returned and they must be done differently for 2.5.x and 3.x 
in 3.x you must catch (Exception




On Sun, Mar 31, 2013 at 3:51 AM, Jurian Even <in...@twentronix.com> wrote:
FOF already includes JSON, you might want to check it out (of course there's more logic in FOF which is needed to accomplish this):
https://akeeba.assembla.com/code/fof/git/nodes/master/fof/view/json.php

Op donderdag 21 maart 2013 15:21:56 UTC+1 schreef Evandromar Machado het volgende:
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Best Regards
Dan Casky
Youjoomla Customer Service
+1727-388-6653
1670 Southgate Mill DR NW
Duluth ,GA
30096
-------------------------------
www.youjoomla.com
Professional Joomla Web Design Services
Reply all
Reply to author
Forward
0 new messages