Joomla 3 JQuery json response

425 views
Skip to first unread message

Mind gem

unread,
Sep 13, 2015, 5:01:54 AM9/13/15
to Joomla! General Development
Hello,

I'm trying to customize a certain component to use JSON and the system-message-container to get the response. Resources I tried to follow are:

http://joomlablog.ru/dokumentatsiya/dlya-razrabotchika/api/542-jresponsejson-vozvrashchaem-dannye-v-formate-json

And

https://docs.joomla.org/JSON_Responses_with_JResponseJson

But I cant get it to work.

My scripts:

jQuery(function($) {

$(document).ready(function() {

$('button.submit').on('click', function(){
$('#vote-form').submit();
return false;
});

$('#vote-form').submit(function(event) {

var domForm = document.getElementById('vote-form');

if (!document.formvalidator.isValid(domForm)) {

return false;

}

console.log(url);

$.getJSON(index.php?option=com_polls&task=poll.vote&format=json', {data: data})
.done(function(r) {
if (!r.success && r.message)
{
alert(r.message);
}
if (r.messages)
{
Joomla.renderMessages(r.messages);
}
if (r.data)
{
alert(r.data.myfirstcustomparam);
alert(r.data.mysecondcustomparam);
}
})
.fail(function() {
alert('Ошибка Ajax запроса!');
})
.always(function() {
alert('Ajax запрос завершен');
});




return false;

});

});
});

My controller part:

public function vote() {

JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

$app = JFactory::getApplication();
$model = $this->getModel('poll');
$params = JComponentHelper::getParams('com_polls');
$user = JFactory::getUser();
$id = $app->input->getInt('poll_id', 0);

$app->enqueueMessage('Some error message', 'warning');
$result = true;
echo new JResponseJson($result, 'blbblbl');
jexit();

}

This echo's the json response in a reloaded blank page. The console.log gives me the right expected url.

Anyone could point me in the right direction?

Fedir

unread,
Sep 13, 2015, 1:36:15 PM9/13/15
to Joomla! General Development
>  $.getJSON(index.php?option=com_polls&task=poll.vote&format=json', {data: data}) 

> JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); 

these two parts is the cause of your problem,

At AJAX request you do not send the token, but you tries to check the in your controller,
actually there some more undefined variables in your JavaScript code



неділя, 13 вересня 2015 р. 12:01:54 UTC+3 користувач Mind gem написав:

Mind gem

unread,
Sep 13, 2015, 1:52:32 PM9/13/15
to Joomla! General Development
I commented out the lines before the jresponsejson which aren't needed at the moment (off course not the $app). But still the same problem.

What you mean by "actually there some more undefined variables in your JavaScript code"? Which ones that are relevent for this test and could make it echo on blank page?


Op zondag 13 september 2015 11:01:54 UTC+2 schreef Mind gem:

Fedir

unread,
Sep 15, 2015, 2:29:40 AM9/15/15
to Joomla! General Development
example I do not see where defined: data in {data: data} and "url" in console.log(url); 
check you browser console for more details

неділя, 13 вересня 2015 р. 20:52:32 UTC+3 користувач Mind gem написав:

Mind gem

unread,
Sep 15, 2015, 3:25:00 AM9/15/15
to Joomla! General Development
the data var isn't required for the getJSON? I thought it was the form data.
I will check later this day if that makes a difference and let know.

Thnx!



Op zondag 13 september 2015 11:01:54 UTC+2 schreef Mind gem:
Hello,

Allon Moritz

unread,
Sep 15, 2015, 3:35:32 AM9/15/15
to joomla-de...@googlegroups.com
I needed it for my attachments extension. To send the message I use the following code https://github.com/Digital-Peak/DPAttachments/blob/master/com_dpattachments/admin/helpers/dpattachments.php#L61. On the javascript side I do a jQuery.ajax call (you have much more configuration options).

--
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 email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.

Mind gem

unread,
Sep 15, 2015, 1:28:55 PM9/15/15
to Joomla! General Development
I looked at your upload script in dp but only could find a minifed js? Do you have a readable example of the ajax js you use?


Op zondag 13 september 2015 11:01:54 UTC+2 schreef Mind gem:
Hello,

Mind gem

unread,
Sep 18, 2015, 11:08:41 AM9/18/15
to Joomla! General Development
So I've got the getjson part working now. Does anyone have an example of how I give setRedirect from my controller to javascript and redirect my page after jresponsejson gave a success back?


Op zondag 13 september 2015 11:01:54 UTC+2 schreef Mind gem:
Hello,

Viper

unread,
Sep 18, 2015, 2:26:10 PM9/18/15
to Joomla! General Development
In javascript do document.location.href = 'new url';
Reply all
Reply to author
Forward
0 new messages