Hacks: Solving ajax giving cross-domain error or responding empty

Posted by: culexon 2012/3/9 18:23:20 9733 reads When assigning XOOPS_URL to a javascript variable in eighter preloads or specific pages.

Most often the browser will deny your request and return only an empty respond. But not give error. This can be seen in FireBug's console when the requests is returning code "200 OK" but text is in red color = empty result. Very annoying

The problem is between Xoops Cookie and browser. I noticed this when being logged in to a xoops page and closing the page without logging out. When re-opened the ajax running in my preloads (in this case XIM and Smallworld) was denying sending messages or posting to the wall.

Also this is a problem if you log out from http://domain.com and open http://www.domain.com.

There are as I see this 2 solutions.

1) Everytime you finish browsing do log out.

Another solution is to define the javascript var used in ajax requests according to actual browser adress.

Add a function to your page (if you add to include/functions.php remember to include this page in preloads or the page your working on)

/**
     * @Get url of smallworld
     * @returns string
     */ 
    
function smallworld_getHostRequest() 
    {
        
$protocol strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === FALSE 'http' 'https';
        
$host     $_SERVER['HTTP_HOST'];
        
$script   $_SERVER['SCRIPT_NAME'];
        
$params   $_SERVER['QUERY_STRING'];
        
$currentUrl $protocol '://' $host;
        return 
$currentUrl;
    }


In your page reset the var like this
// Check if request url is with www or without
        
global $xoTheme;
        
$urltest smallworld_getHostRequest();
        
$xoops_url XOOPS_URL;
        if (!
strstr($urltest'www.')) {
            
$xoops_url str_replace'www.'''$xoops_url );
        }

        
$script "var smallworld_url = '" $xoops_url "/modules/smallworld/" "';n";
        
$xoTheme->addScript('','',$script);


The var smallworld_url will now be according to your browser adress and this way not result in ajax cross-browser error.