?

Log in

No account? Create an account

Wed, Aug. 31st, 2005, 01:10 pm
all the cool kids are AJAXing - why can't I?

We have some user images on a server (let's call it 'a') at work and I wanted to automatically inline them on another page on another server (which, for demonstration purposes we shall call 'b') if they exist. This, I felt, was a job for this AJAX malarkey that I'd heard so much about.

This

    <html>                                                                              
    <head>                                                                              
    <script type="text/javascript">                                                     
    function picture(name)                                                              
    {                                                                                   
        var xmlhttp=false;                                                              
    /*@cc_on @*/                                                                        
    /*@if (@_jscript_version >= 5)                                                      
        // JScript gives us Conditional compilation                                     
        // so we can cope with old IE versions.                                         
        // and security blocked creation of the objects.                                
        try {                                                                           
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");                              
        } catch (e) {                                                                   
            try {                                                                       
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");                       
            } catch (E) {                                                               
                xmlhttp = false;                                                        
            }                                                                           
        }                                                                               
    @end @*/                                                                            
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {                           
            xmlhttp = new XMLHttpRequest();                                             
        }                                                                               
                                                                                    
        var url     = "http://a/"+name+".jpg";                                
        xmlhttp.open("HEAD", url,true);                                                 
        xmlhttp.onreadystatechange=function() {                                         
            if (xmlhttp.readyState==4) {                                                
                if (xmlhttp.status==200) {                                              
                    document.write("<img src='"+url+"'>");                              
                }                                                                       
            }                                                                           
        }                                                                               
        xmlhttp.send(null);                                                             
    }                                                                                   
    </script>                                                                           
    </head>                                                                             
    <body>                                                                              
                                                                                    
    <div>                                                                               
          <script type="text/javascript">                                                   
            picture('foobar')                                                           
          </script>                                                                         
    </div>                                                                              
                                                                                    
    <div>                                                                               
          <script type="text/javascript">                                                   
              picture('doesnt exist')                                                       
          </script>                                                                         
    </div>                                     
    </body>
    </html>

didn't work, throwing up errors like

    "uncaught exception: Permission denied to call method XMLHTTRequest.open"

To get round the security thing I had to stick a script on b

    #!/usr/bin/perl -w                                                                  
                                                                                    
    use strict;                                                                         
    use CGI;                                                                            
    use LWP::Simple;                                                                    
                                                                                    
    $|++;                                                                               
                                                                                    
    my $q = CGI->new;                                                                   
    my $n = $q->param('url');                                                          
    my $u = "http://a/${n}.jpg";                                              
    if (head($u)) {                                                                     
        print $q->header('text/html');                                              
    } else {                                                                            
        print $q->header('text/html', '404 Not Found');                             
    }                                                                                   
                                                                                    
    print "\n";                                                                         
    exit 0; 

And change the line in the javascript to be

    xmlhttp.open("HEAD", "/proxy_check?url="+url,true); 

And lo! It worked. Huzzah!