By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

Interfacing Javascript with CGI

P: n/a
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.

Aug 31 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


gm*******@gmail.com wrote:

<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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 31 '05 #2

P: n/a
Thanks. I tried "text/javascript" as well, but it didn't work either.

Aug 31 '05 #3

P: n/a
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

gm*******@gmail.com wrote: 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.


Sep 1 '05 #4

P: n/a
gm*******@gmail.com wrote:
Thanks. I tried "text/javascript" as well, but it didn't work either.


Perhaps if you used PATH_INFO it would work better.
Just stick a fake filename ending in ".js" after the .cgi filename.
Do use the content-type suggestions as well.

<script type="text/javascript" language="JavaScript"
src="/cgi-bin/cookie/script1.cgi/fakename.js">
</script>
--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML/CSS,Javascript,TCP ...
--`
Sep 4 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.