heatblazer has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks
Recently I was trying to rewrite the sucking php mailform and replace it with cgi/perl. I got a werid error 403 when XMLHttpRequest is sending my variables. Here is some code

AJAX

var dataObj = { 'do0' : '', // name 'do1' : '', //email 'do2' : '', //sunject 'do3' : '' // text }; /* create the new xmlhttp req here */ function createXMLHttpRequest() { var http = false; if ( window.XMLHttpRequest ) { try { http = new XMLHttpRequest(); } catch (e) { http = false; } } else if ( window.ActiveXObject ) { try { http = new ActiveXObject("Msxml2.XMLHttp"); } catch (e) { try { http = new ActiveXObject("Microsoft.XMLHttp"); } catch (e ) { http = false; } } } return http; } // simple testing assumes everything is ok :) window.onload = function() { document.getElementById("sender").onclick = function() { for ( var i=0; i < 4; i++) { dataObj['do'+i] = document.getElementById("do"+i).valu +e; //alert(dataObj['do'+i]); } var nr = new XMLHttpRequest(); var sent = "cgi-bin/ex2.pl?name="+dataObj['do0']+"&email=" ++dataObj['do1']+"&subject="+dataObj['do2']+"&text="+dataObj['do3']; nr.open('post', sent, true); nr.send(); } }

And the CGI...

CGI

#!/opt/lampp/bin/perl use strict; use warnings; use CGI; my $p = new CGI; my ($name, $email, $sub, $txt) = ( $p->param('name'), $p->param('email'), $p->param('subject'), $p->param('text'), ); $p->header; print "$name\n$email\n$sub\n$txt\n"; exit(0);

In my firebug the POST is sended but it shows me: POST http://localhost/cgi-bin/ex2.pl?name=c&email=a&subject=b&text=ff 403 Forbidden

Can you help me here? I have no idea why is this happening, I`ve chmodede my perl script with 777 and 755 no effect... I am running on arch linux XAMPP btw.

Fixed

It appears that adding this:

<Directory "/opt/lampp/cgi-bin"> AllowOverride None Options ExecCGI # this one Order allow,deny Allow from all </Directory>

to httpd.conf fixes the issue

Replies are listed 'Best First'.
Re: Ajax and CGI problem
by aaron_baugher (Curate) on Mar 05, 2012 at 18:49 UTC

    Check your web server's error log. 403 Forbidden means what it says -- forbidden -- possibly a permissions issue, but it could be several different things: permissions on the directory (not just the script itself), web server configuration not allowing CGIs in this location, something denied in htaccess, etc. See the server log for details.

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

      I`ve tried to ran it as sudo even with sudo browse. I`ve checked the error log but did not get it at all...

      [Mon Mar 05 20:38:07 2012] [error] [client 127.0.0.1] script not found + or unable to stat: /opt/lampp/cgi-bin/ex2, referer: http://localhost +/form1.html [Mon Mar 05 20:46:15 2012] [error] Options ExecCGI is off in this dire +ctory/opt/lampp/cgi-bin/ex2.pl

      Any suggestions?

        The server error is fairly explicit about what's wrong: Options ExecCGI is off in this directory. Set it on in a config file apache will see at startup.

Re: Ajax and CGI problem
by Your Mother (Archbishop) on Mar 05, 2012 at 18:53 UTC

    Among the possible problems, you're not sending the headers.

    $p->header; # Needs to be printed.
      It`s removed, I forgot it when I copy paste it... Ignore that line.