Simon Wistow (deflatermouse) wrote,
Simon Wistow

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.


    <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";                               "HEAD", url,true);                                                 
        xmlhttp.onreadystatechange=function() {                                         
            if (xmlhttp.readyState==4) {                                                
                if (xmlhttp.status==200) {                                              
                    document.write("<img src='"+url+"'>");                              
          <script type="text/javascript">                                                   
          <script type="text/javascript">                                                   
              picture('doesnt exist')                                                       

didn't work, throwing up errors like

    "uncaught exception: Permission denied to call method"

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"HEAD", "/proxy_check?url="+url,true); 

And lo! It worked. Huzzah!

Tags: ajax, huzzah, images, javascript, job, malarkey

  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.