MarsHut Page 1/8

yrekazzyzxamuckInternet και Εφαρμογές Web

4 Δεκ 2013 (πριν από 3 χρόνια και 7 μήνες)

280 εμφανίσεις

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?
Thanks
Archives
Framework
Joomla
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()) {
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ë«=event.move',
data: {id: event.id},
success: function (data) {
jQuery('#loader').hide();
var json = jQuery.parseJSON(data);
// do some stuff
Joomla.renderMessages(json.messages);

});

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

Dmitry
Answer by
Allon Moritz
on 2013-03-22T05:58:56-04:00
It is a gracefull die...
Answer by
Piotr_cz
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:
https://github.com/joomla/joomla-platform/pull/1596

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
Piotr_cz
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
use.
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 ] gmail.com
Reply-To: joomla-dev-general [ at ] googlegroups.com
MarsHut
Page 2/8
Joomla raiseError with AJAX get message
Date: Friday, March 22, 2013 6:54 AM
To: joomla-dev-general [ at ] googlegroups.com
Subject: Re: [jgen] Re: Joomla raiseError with AJAX get message

You will have my vote!



$data

database

send

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

Thanks,
David Hurley
Answer by
Chraneco
on 2013-03-22T10:27:42-04:00
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ë¶é²_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
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
them.
MarsHut
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
return!
It would be great to have that back.
I believe that by adding that the platform would be very good.
Answer by
Piotr_cz
on 2013-03-22T13:31:46-04:00
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/i
ndexer.json.php

On Mar 22, 4:34 pm, Evandromar Machado
wrote:
Answer by
Piotr_cz
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:
https://docs.google.com/document/d/1FVKGlV6BN6pu-YH2WR2pQHE3Ez7M6r7LD417GSw9ZSo/e
dit#
MarsHut
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:

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.
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
perfectly.
Joomla.renderMessages (data.messages) had no effect why?
The error messages were instanceof Exception, I changed the code below and
MarsHut
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 =
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;
Answer by
Aymeric Dourthe
on 2013-03-22T13:58:00-04:00
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 :
Answer by
Piotr_cz
on 2013-03-27T06:39:19-04:00
This is interesting:

j3-rest-api
MarsHut
Page 6/8
Joomla raiseError with AJAX get message
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#
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):
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:
Answer by
Youjoomla.com
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
http://www.yjsimplegrid.com/

go to elements folder and get yjsgjson.php , it is custom 50 lines JSON
class from Contsantin,
http://www.youjoomla.com/meet-the-team.html
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
Tagged
Error Response
Joomla Raiseerror
Json Format
Php Raiseerror Calls
Treating Errors
Related
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.
MarsHut
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
http://www.marshut.com/kwyx/joomla-raiseerror-with-ajax-get-message.html
MarsHut
Page 8/8