2

I have a plugin that's doing Ajax calls. It constructs a FQDN for making the call using JURI::base().

What's happening is sometimes is using www.somedomain.com, and other times it's using a different virtual host name www.somedomain.st.us.

www.somedomain.st.us has a CNAME dns record that points to www.somedomain.com.

You can see a host (not the one demonstrating the problem) at:

http://joomla.connectdaily.com/index.php/calendar

That page displays a calendar. The AJAX code that handles the navigation arrow clicks is around line 189 of the page source.

The PHP code that creates the URL is:

public function getAjaxURL($addlParms=null){
  $result=$this->getSiteUrl().'index.php?option=com_ajax&plugin=connectdaily&group=content';
  if ($addlParms!=null) {
    $result.='&'.$addlParms;
  }
  return $result;
}

public function getSiteUrl() {
  $res=JURI::base();
  if (strpos($res,"/administrator/")>0) {
     $res=substr($res,0,strlen($res)-14);
  }
  return $res;
}

Sometimes getSiteURL() returns www.somedomain.com, and other times (it seems random) it returns www.somedomain.st.us.

Does anyone have any experience or knowledge about this? Can one plugin or module change the value returned?

1
  • Could you show the code for you're using for your Ajax request?
    – Lodder
    Aug 19 '16 at 18:57
2

The better way is to use JRoute which will take care of getting right url for you.

$url = JRoute::_('index.php?option=com_ajax&plugin=connectdaily&group=content', false, -1);
$url = str_replace('/administrator/', '/', $url);

Please note the third parameter of JRoute, it is given intentionally to get the full site name instead of relative url. You can ommit 2nd and 3rd parameters if you don't want absolute url.

1

I figured out what the real issue was here. For performance reasons, my code saves generated content in a local cache. So, the sequence of events was:

User A requests: http://www.somedomain.st.us/calendar/ and receives content which is stored in the cache.

User B requests: http://www.somedomain.com/calendar/ and receives cached content generated for user A with an AJAX url of http://www.somedomain.st.us.

When User B clicks on a control, the AJAX request fails because it's cross-domain.

For now, I'm going to try using the relative URL for AJAX. I've encouraged the customer to stop using a virtual host/CNAME for www.somedomain.st.us and to instead configure a redirect for www.somedomain.st.us to www.somedomain.com.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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