Interfacing Javascript with CGI

G

gmazujian

I have a problem. I would like to call a cgi program from javascript.
I have scanned the web. I have a cgi program. It works fine if I call
it by the url. However, when I call it from the javascript script it
appears to do nothing. It doesn't generate any error.
First, Here is my html with Javascript.

<script type="text/javascript" language="JavaScript"
src="/cgi-bin/cookie/script1.cgi">
</script>


Second, here is my cgi program.

#!/usr/local/bin/perl
# CGI program can do other stuff here.
print "Content-type: text/html\n\n";
print "<!--\n";
print "var x;\n";
print "document.write('The number of visits is: ');";
print "//-->\n";
# end of program

It would be greatly appreciated if someone could tell me what is going
wrong with it. By the way, this is on a FreeBSD machine running Apache
1.3.3.
 
M

Martin Honnen

<script type="text/javascript" language="JavaScript"
src="/cgi-bin/cookie/script1.cgi">
</script>
#!/usr/local/bin/perl
# CGI program can do other stuff here.
print "Content-type: text/html\n\n";

If you want to send JavaScript code then do that and use a proper
Content-Type, e.g. text/javascript or application/x-javascript.
print "<!--\n";

That starts an SGML or XML comment and does not belong into pure
JavaScript code. If you had a static .js file you would include with
<script src="file.js"> then you hopefully don't put it in there either.
 
P

Peroli

Hi,
The problem is with the "content-type". Set it to "text/plain". Js
files are text files and should not be treated as HTML or anything
else.
print "Content-type: text/html\n\n";

change it to
print "Content-type: text/plain\n\n";

- Peroli Sivaprakasam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top