Question : Calling perl cgi from javascript

In code section you will see html code that makes a button that calls a javascript function that sends a request.

The request is to a perl script  in cgi-bin.

When I call testCGI.pl from browser it works.

But when I call from the button in the html I get an alert window showing the perl code in the testCGI.pl.

Permissions of testCGI.pl are 755.

My question:
 What do I have to do to call cgi from html using javascript?

Ross
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
      <title>Hello World introduction to Ajax</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <script type="text/javascript" src="ajax.js"></script>
</head>

<body>
      <p id="mySentence"><a href="#" onclick="javascript:sndReq();">Click here</p>
</body>

</html>



#!/usr/local/bin/perl
use CGI;
use CGI qw/:standard/;

my $q= new CGI;

my $pest_name = $q->param("pest");

print $q->header ("text/html"),
$q->start_html("welcome"),
$q->p("From CGI-Bin"),
$q->p("$pest_name"),
$q->end_html;



function createRequestObject() { 
var ro; 
var browser = navigator.appName; 
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP"); 
}else{
ro = new XMLHttpRequest(); 
} 
return ro;
}

var http = createRequestObject();



function sndReq() {
http.open('POST', 'cgi-bin/testCGI.pl');
http.onreadystatechange = handleResponse;
http.send(null);
}



function handleResponse() { 
if(http.readyState == 4){ 
var response = http.responseText;
alert(response);
document.getElementById('MessBox').value = response; 
}
}

Answer : Calling perl cgi from javascript

>>  after restarting gets an error  System.ArgumentNullException' occurred in mscorlib.dll

This is because your file is blank and ReadLine() returns nothing, so null is being passed to Long.Parse(). You should consider checking the return of ReadLine() for null before passing to Long.Parse--even if you get the logic working correctly.

>>  it all works except when it tries to write to the file nothing gets written

What kind of app is this? A windows service?

>>  what os are you using

XP
Random Solutions  
 
programming4us programming4us