0

I have an ajax task to return a form token from my controller for use in ajax pages, so that subsequent ajax requests will have a token. It looks like this in the code:

JURI::root().'index.php?option=com_mycomponent&format=raw&task=ajax.tokenRequest'

and like this in the ajax request:

http://localhost/joomla3/index.php?option=com_mycomponent&format=raw&task=ajax.tokenRequest

This works fine, until I use it in a multilingual site. In this case, the server returns a 303 error with the suggested redirect to

http://localhost/joomla3/en/?option=com_mycomponent&format=raw&task=ajax.tokenRequest

and the token never gets populated.

Is there something I need to do similar to JRoute::_() on the URL to ensure that the right URL is used and a 303 is not returned? (I did try JRoute but this didn't have any effect).

Thanks

2 Answers 2

0

Instead of JUri::root() try to use this:

$baseUrl = JUri::getInstance()->toString(array('scheme', 'user', 'pass', 'host', 'port', 'path'));
$baseUrl .'index.php?option=com_mycomponent&format=raw&task=ajax.tokenRequest'

See the reference.

Or simply do not use JUri::root().

0

First of all it would be nice to use the JSession::getFormToken() method to increse the security of your AJAX request. See also How to add CSRF anti-spoofing to forms

It is not very clear from your question, but it looks to me that you are not passing the language to the Request (like specifying the language parameter lang=de).

Also have a look into VirtualHosts for your development environment, so that you can have something like http://mycooljoomlasite.local instead of http://localhost/joomla3

2
  • I'm using the JSession::getFormToken() at the ajax endpoint to get the token. This is necessary because if you simply declare the variable in js (var token = JSession etc) it will be cached (if Joomla caching is on) and your token will always be invalid.
    – user101289
    Commented Feb 3, 2015 at 20:43
  • That is interesting. Did you test it? Commented Feb 4, 2015 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.