Joomla redirect after ajax request fails

542 views
Skip to first unread message

Sir Perino

unread,
Jul 2, 2014, 8:57:24 AM7/2/14
to joomla-de...@googlegroups.com
An ajax request calls a controller function within my MVC Component.
And in this function there is a Joomla redirect at the end with

JFactory::getApplication()->redirect(JRoute::_($redirect_url, false));

The problem is, that the redirected page won't open! While debugging a session, I can see that the controller will initiate the redirect and the proper details will be sent to view.html.php. But the page, in this case it is a different view in tmpl, will not be open!

Is this because of the ajax call? Do I have to do something in the ajax success part?

Viper

unread,
Jul 2, 2014, 1:24:12 PM7/2/14
to joomla-de...@googlegroups.com
Joomla use different views for html/json/raw/pdf/etc requests. If you're trying to redirect user to another page if ajax request fail - use document.location.href = 'new_url.php' in the template.

Brad Gies

unread,
Jul 2, 2014, 2:35:26 PM7/2/14
to joomla-de...@googlegroups.com

What code are you using for success in your ajax call? Can you post it?
--
Sincerely,

Brad Gies
----------------------------------------------
bgies.com maxhomevalue.com
idailythought.com greenfarminvest.com
----------------------------------------------

Bakual

unread,
Jul 2, 2014, 4:01:22 PM7/2/14
to joomla-de...@googlegroups.com
I don't think a redirect will work when using an AJAX call, they are browser redirects. You would have to initiate the browser page reload yourself in your JavaScript.

Sir Perino

unread,
Jul 3, 2014, 3:25:11 AM7/3/14
to joomla-de...@googlegroups.com
In the form I have several 'buttons'. These are not really buttons, they are images with href and onlick function, which are initialising the ajax call. Submit buttons or Function would be prefered, but not possible, because I need to transfer one value (productnr). And each 'button' has its own product nr. So form submission can not be done, instead I make a jquery ajax call to address the controller function. But finally I need the redirect, which will normal be done, by the last statement in the controller function ->redirect(JRoute::_....)

My ajax script:
jQuery(document).ready( function() { jQuery(".productorder").click(function (SetOrder) { var order = jQuery(this).find("#productnr").attr("value"); var formdata = new FormData(); formdata.append("productnr", order); jQuery.ajax({ type: "POST", dataType: "json", data: formdata, processData: false, contentType: false, url: "index.php?option=com_component&task=controller.function", success:function(result) { } }); }); });

In the success part I can do window.location = result.data but the url wich came from the controller is not correct.
The controller part for this is

$redirect_url = "index.php?option=com_mycomponent&view=my_view&layout=different_layout";
// JFactory::getApplication()->redirect(JRoute::_($redirect_url, false)); => this seems not really work i with the ajax call
echo new JResponseJson($redirect_url);

Maybe Iam on a wrong way and a different solution would be better?! In my view i need several buttons which are displayed with an image and should transfer a value (productnr) to the controller function. After submitting the form, I need to go to a different form.



Am Mittwoch, 2. Juli 2014 14:57:24 UTC+2 schrieb Sir Perino:

Bakual

unread,
Jul 3, 2014, 5:30:13 AM7/3/14
to joomla-de...@googlegroups.com
Forms usually aren't loaded by directly accessing the view/layout. It goes through the "&task=edit" part. This makes some access checks and set a session parameter which tells the form that it's allowed to be loaded. Otherwise you get an error. Maybe that's the issue?
Other than that, why don't you just create the correct URL yourself in your own controller?

Sir Perino

unread,
Jul 3, 2014, 6:09:35 AM7/3/14
to joomla-de...@googlegroups.com

Currently the pages are displayed with SEF Urls. In the development enviroment this means e.g.

localhost/project/controllcenter/ordering

When I set in the controller
$redirect_url = "index.php?option=com_mycomponent&view=my_view&layout=different_layout";

and transfer that url back as Ajax result, then two problems arise.

first: I left the SEF Url Mode and a normal Url will be displayed in the following page, but I would like to keep in SEF Url Mode
second: the page will be redirected to localhost/project/controllcenter/index.php?option=com_mycomponent&view=my_view&layout=different_layout

In the string above, the part 'controllcenter' would be too much. The correct Url would be
localhost/project/index.php?option=com_mycomponent&view=my_view&layout=different_layout



Am Mittwoch, 2. Juli 2014 14:57:24 UTC+2 schrieb Sir Perino:

Bakual

unread,
Jul 3, 2014, 10:22:14 AM7/3/14
to joomla-de...@googlegroups.com
To get SEF URLs you just need to use JRoute::_() and pass the non-SEF URL as argument. See http://api.joomla.org/cms-3/classes/JRoute.html

"controllcenter" is likely the alias of the active menu item. In Joomla you should almost always have an "Itemid" within the URL, so Joomla knows which menu settings to load.

Sir Perino

unread,
Jul 3, 2014, 11:01:23 AM7/3/14
to joomla-de...@googlegroups.com
That sounds good.

How can I get Joomlas JRoute::_(....) in my Ajax success script to run or should I do this string combination in the php-file? But what should I send
then back to ajax?




Am Mittwoch, 2. Juli 2014 14:57:24 UTC+2 schrieb Sir Perino:

Bakual

unread,
Jul 3, 2014, 12:01:59 PM7/3/14
to joomla-de...@googlegroups.com
You can't do that in your JavaScript code. It needs to be done within your controller and you send the SEF URL back to your script together with whatever other data you need sent to the client.

Sir Perino

unread,
Jul 3, 2014, 2:49:59 PM7/3/14
to joomla-de...@googlegroups.com
Thank you for your attention! The redirect now is working!

The only thing which do not operate proper, is the SEF Url translation.

In the php File I declare the redirect URL:

$redirect_url = "index.php?option=com_mycomponent&view=my_view&layout=different_layout&itemid=408";
$redirect_url = JRoute::_($redirect_url, false);

this will end up in the url "localhost/project/component/nameofthecomponent_without_com/view=my_view/layout=different_layout&itemid=408"

But the SEF Url link should be "localhost/project/controllcenter/name_of_menue_item" displaying with layout=different layout

What's wrong here?




Am Mittwoch, 2. Juli 2014 14:57:24 UTC+2 schrieb Sir Perino:

Bakual

unread,
Jul 3, 2014, 5:21:43 PM7/3/14
to joomla-de...@googlegroups.com
Make it "Itemid" and not "itemid". The uppercase "I" is important.

Sir Perino

unread,
Jul 4, 2014, 2:26:21 AM7/4/14
to joomla-de...@googlegroups.com

That seems to be the right way! Thx!

The given URL path is now localhost/project/controllcenter/alias_of_menue_item&view=viewname&layout=layoutname

Is there a possibility to hide the part &view=viewname, maybe also the part &layout=layoutname?



Am Mittwoch, 2. Juli 2014 14:57:24 UTC+2 schrieb Sir Perino:

Bakual

unread,
Jul 4, 2014, 2:47:00 AM7/4/14
to joomla-de...@googlegroups.com
Sure, you need a component router file then. See http://docs.joomla.org/Supporting_SEF_URLs_in_your_component
Reply all
Reply to author
Forward
0 new messages