MarsHut Page 1/8

yrekazzyzxamuckInternet and Web Development

Dec 4, 2013 (5 years and 2 months ago)


Joomla raiseError with AJAX get message
Asked by
Evandromar Machado
on 2013-03-21T10:21:56-04:00
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?
Mar 21, 2013
Week 12, 2013
March, 2013
Year 2013
All Answers
Answer by
Allon Moritz
on 2013-03-21T15:49:55-04:00
I'm using this in my controller
public static function sendMessage($message, $error = false, array $data =
array()) {

if (!$error) {
echo new JJsonResponse($data);
} else {
JFactory::getApplication()->enqueueMessage($message, 'error');
echo new JJsonResponse($data);


And in the js file something like
type: 'POST',
url: 'index.php?option=com_demoë«=event.move',
data: {id:},
success: function (data) {
var json = jQuery.parseJSON(data);
// do some stuff


Page 1/8
Joomla raiseError with AJAX get message
Hope that helps....

On Thu, Mar 21, 2013 at 3:21 PM, Evandromar Machado
Answer by
Dmitry Rekun
on 2013-03-22T01:59:25-04:00
That's interesting laoneo. What is the purpose of closing an application?

Answer by
Allon Moritz
on 2013-03-22T05:58:56-04:00
It is a gracefull die...
Answer by
on 2013-03-22T06:12:42-04:00
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:

Do you think we can revive it?
Answer by
Allon Moritz
on 2013-03-22T07:12:46-04:00
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.
Answer by
on 2013-03-22T07:35:16-04:00
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
Answer by
Allon Moritz
on 2013-03-22T07:54:48-04:00
You will have my vote!
Answer by
Michael Babker
on 2013-03-22T09:15:51-04:00
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.moritz [ at ]
Reply-To: joomla-dev-general [ at ]
Page 2/8
Joomla raiseError with AJAX get message
Date: Friday, March 22, 2013 6:54 AM
To: joomla-dev-general [ at ]
Subject: Re: [jgen] Re: Joomla raiseError with AJAX get message

You will have my vote!




Answer by
David Hurley
on 2013-03-22T09:21:28-04:00
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.

David Hurley
Answer by
on 2013-03-22T10:27:42-04:00

when I created that pull request to the platform
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:!topic/joomlabugsquad/YhxiAL5iaf8
and a tracker issue here:ë¶é²_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();

Answer by
Evandromar Machado
on 2013-03-22T11:34:33-04:00
I got so many calls in my ajax component that would be difficult to count
Page 3/8
Joomla raiseError with AJAX get message
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
It would be great to have that back.
I believe that by adding that the platform would be very good.
Answer by
on 2013-03-22T13:31:46-04:00
Using JJResponseJson, you'd do:


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

On Mar 22, 4:34 pm, Evandromar Machado
Answer by
on 2013-03-22T13:52:06-04:00
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:
Page 4/8
Joomla raiseError with AJAX get message

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
Answer by
Alonzo Turner
on 2013-03-22T14:47:51-04:00
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:

type: 'POST',
url: 'index.php',
data: { option: 'com_myextension', view: 'myview', format: 'json' },
success: function(data)

return false;

failure: function(data)



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.
Answer by
Evandromar Machado
on 2013-03-22T15:22:40-04:00
Hello, forgive me if I'm doing it wrong!!
I managed to capture responses when exceptions occur, could make it work
Joomla.renderMessages (data.messages) had no effect why?
The error messages were instanceof Exception, I changed the code below and
Page 5/8
Joomla raiseError with AJAX get message
now everything works perfectly, the co code below.
Thanks for the help and all.

public static function sendMessage($message, $error = false, array $data =

if (!$error)

echo new JJsonResponse($data);


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;


In script ...

success: function (data){
if (data) {
if (!data.success) {
return false;
Answer by
Aymeric Dourthe
on 2013-03-22T13:58:00-04:00

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 :
Answer by
on 2013-03-27T06:39:19-04:00
This is interesting:

Page 6/8
Joomla raiseError with AJAX get message

Web services proof-of-concept code
Answer by
Jurian Even
on 2013-03-31T03:51:37-04:00
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):

Op donderdag 21 maart 2013 15:21:56 UTC+1 schreef Evandromar Machado het
Answer by
on 2013-03-31T11:50:51-04:00
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


on line 49 you will see
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
Error Response
Joomla Raiseerror
Json Format
Php Raiseerror Calls
Treating Errors
Error in Joomla component
Joomla problem at all admin sidebar, after uninstall theme hathor!!!
doubt jForm - setFieldAttribute unset
How to use format=json
Editing records with id / key negative.
Page 7/8
Joomla raiseError with AJAX get message
Getting a Module AJAX Response
data format: to json or not to json?
Get error from plugin
View Online
Page 8/8