SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XMLHttpRequest and subdomains

    I had just gotten subdomains to work (username.domain.com) and then realized my Ajax scripts weren't doing that good anymore.

    I read that XMLHttpRequest can't access other domains, but shouldn't there be a simple(r) way to make it work with subdomains?

    I'm pretty much a javascript newbie but decent with PHP.

    Is there a painless way to make this all work again? Or any other way?

    Here's a bit of the code;
    Code JavaScript:
    var cururl = 'http://domain.com/';
     
    function createRequestObject() {
    	var req;
     
    	if (window.XMLHttpRequest) {
    		req = new XMLHttpRequest();
    	} else if (window.ActiveXObject) {
    		req = new ActiveXObject("Microsoft.XMLHTTP");
    	} else {
    		alert('Problem creating the XMLHttpRequest object');
    	}
     
    	return req;
    }
     
    function handleDivTag(divtag) {
    	var divtag;
    	return divtag;
    }
     
    var http = createRequestObject();
    var divhandler = new handleDivTag(null);
     
    function sendRequest(ua_id,show,series) {
    	http.open('get', cururl+'status.php?ua_id='+ua_id+'&show='+show+'&series='+series+'&dummy=' + new Date().getTime());
    	http.onreadystatechange = handleResponseTwo;
    	divhandler.divtag = ua_id;
    	http.send(null);
    }
     
    function handleResponseTwo() {
    	if (http.readyState == 4 && http.status == 200) {
    		var response = http.responseText;
    		if (response) {
    			document.getElementById('editinfo'+divhandler.divtag).innerHTML = response;
    		}
    	}
    }

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Can you make your script accessible from the subdomains (a symlink on the file system perhaps)? That'd be the easiest way, since the cross-site scripting restrictions are imposed by the browsers, which are out of our control.

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would you please give a bit more info on how I could use that, I looked it up and I guess it's not as easy as just writing symlink(http://domain.com,currentuser.domain.com) in the PHP file where the the sendRequest() links are located? I guess I can't use absoulte paths in symlink tho. And these are wilcard subdomains, so they aren't connected to a specific folder on the server, if I have understood everthing right that is...

    I'm on regular webhosting so I don't think I have the rights to go very deep in the configuration.

    I also tried do solve this with htaccess which I read about in an article;

    RewriteRule ^/call/(.*)$ http://domain.com/$1 [P]

    Placing the status.php file (which is called by the .js file in my first post) in the /call/ directory. Didn't work tho...

  4. #4
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understand symlink correctly, it's something which is just created after having run the command?

    Would something like this work perhaps?
    PHP Code:
    symlink"./",
            
    "/home/web2753/domains/domain.com/public_html/"); 
    I don't really know what to write instead of "./" because the subdomains are wildcard. Don't know if anything bad will happen if I make some wrong moves, as I'm not very skilled with SSL or whatever might be required to remove the symlinks!

    Do you know if symlink is a method which can make this all work? I couldn't really find anything useful when I googled on it (and xmlhttprequest). I've only found methods which seemed to require a lot of rewriting and adding of new javascripts, curl and what not And the above RewriteRule which failed and also ProxyPass which seem to require httpd.conf access.

    I can understand that fetching from other domains/sites should be quite tricky, but I'm leeching off of myself here, so to speak Well, maybe I should just stop whining and drop the subdomains idea, not vital but would probably be an appreciated feature.

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I was referring to creating a symlink on the file system, not any PHP code, and not anything specific to XMLHttpRequest. A symbolic link is like creating a virtual file which is updated whenever the original changes. By creating a symlink to this script you're accessing in each subdomain's directory on the file system, it'd be accessible at an address under the subdomain, so that your XMLHttpRequest can hit a copy on the same domain as the javascript is running, avoiding domain restrictions.

    Look up "ln -s".

  6. #6
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, I see..

    Ok, so I tried a couple of ln -s'es but I can't figure out where to create this symlink. As I don't really have any directories (they are wildcard subdomains).

    Tried this for example;
    Code:
    ln -s /home/web2753/domains/domain.com/public_html/ajax/status.php
    (changed the status.php to /ajax/ directory first)

    This created a symlink for status.php in he same directory as /domain.com/ lies in, and also did the same in the /domain.com/public_html/ directory. This was probably wrong but...

    I also tried setup a directory called /test/ in /public_html/, did the above symlink in /test/, and then tried while entering test.domain.com, but that didn't work either.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •