Hacks

Solving ajax giving cross-domain error or responding empty

culex  09-Mar-2012 17:07 9519 Reads   4 Comment(s) 
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 fromhttp://domain.com and openhttp://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.

Rating 0/5
Rating: 0/5 (0 votes)
Voting is disabled!


Login

Who's Online

253 user(s) are online (7 user(s) are browsing Publisher)


Members: 0


Guests: 253


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Mar 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits

Categories