sudeepdino008 sudeepdino008 - 5 months ago 19
Perl Question

JavaScript not running with Perl CGI

Inside the cgi-bin folder, I have an experiment.cgi:

#!/usr/bin/perl -w

use DBI;
use CGI qw(:standard);
require "../common/config.pl";

print header;
print start_html(-title=>"Add Assignment");


print<<EOT;
<script type = "application/javascript" src = "experiment.js"></script>
EOT

print h2("Add New Automated Assignment");

# print<<AEOT;
# <script>experiment();</script>
# AEOT
print end_html;


In the same folder I have experiment.js

function experiment(){
alert('ghello world');
}


However there is problem with the experiment.js file inclusion. In the log files I get the following error:

End of script output before headers: experiment.js


I searched for this error and found it was relate to cgi files. So my guess is that apache2 is interpreting experiment.js as a cgi file. Am I right in this? If yes, what is the proper way to set it up so that JavaScript and cgi could be used together?

Answer

I searched for this error and found it was relate to cgi files. So my guess is that apache2 is interpreting experiment.js as a cgi file. I am right in this?

Yes

If yes, what is the proper way to setup so that javascript and cgi could be used together.

Don't put non-CGI programs in your cgi-bin. Put them with the rest of your static files.

You'll need to adjust the URL. src="/js/experiment.js" for example.