我想在cgi页面和javascript / Jquery之间传输数据并返回到cgi页面
Index.html具有表单内容
login.cgi有
my $username= $cgi->param('uid');
if (($cgi->param('uid') eq 'demo') && ($cgi->param('pwd') eq 'demo')) {
my $cookie = CGI::Cookie->new(-name=> 'uid', -value=> $cgi->param('uid'));
print $cgi->redirect(-uri => '/cgi-bin/index.cgi', -cookie => $cookie);
index.cgi有
my $uid = $cookies{uid}->value;
print $uid;
print <<"HTML_CODE"
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script src="/scripts/test_javascript.js" type="text/javascript"></script>
</head>
<body>
<h2> This is a Demo Login Page of $uid </h2>
</body>
</html>
HTML_CODE
;
test_javascript.js有
$(document).ready(function () {
var uid = $('#uid').attr('value');
if (uid) {
$.ajax({
url:'/cgi-bin/index.cgi',
data: "uid=" + uid,
// script call was *not* successful
error: function() {
alert("script call was not successful");
},
// script call was successful
// data should contain the string returned by the Perl script
success: function(data){
alert("Your userid is: " + data)
}
});
}
}
)
答案 0 :(得分:0)
$(document).ready(function () {
var uid = $.cookie("uid");
if (uid) {
$.ajax({
url:'/cgi-bin/index.cgi',
data: "uid=" + uid,
// script call was *not* successful
error: function() {
alert("script call was not successful");
},
// script call was successful
// data should contain the string returned by the Perl script
success: function(data){
alert("Your userid is: " + data)
}
});
}
}
)
答案 1 :(得分:0)
Otion 1 - 我会在index.cgi中对其进行硬编码。这样您就可以将值插入到javascript代码中。
这应该是你的index.cgi:
$(document).ready(function () {
var uid = $cookies{uid}->value;
if (uid) {
$.ajax({
url:'/cgi-bin/index.cgi',
data: "uid=" + uid,
// script call was *not* successful
error: function() {
alert("script call was not successful");
},
// script call was successful
// data should contain the string returned by the Perl script
success: function(data){
alert("Your userid is: " + data)
}
});
}
}
)
my $uid = $cookies{uid}->value;
print $uid;
print <<"HTML_CODE"
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script src="/scripts/test_javascript.js" type="text/javascript"></script>
</head>
<body>
<h2> This is a Demo Login Page of $uid </h2>
</body>
</html>
HTML_CODE
;
选项2 - 将$ cookies {uid} - &gt;值分配给隐藏的输入,并让testjavascript稍后捡起来
的index.cgi:
print <<"HTML_CODE"
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script src="/scripts/test_javascript.js" type="text/javascript"></script>
</head>
<body>
<h2> This is a Demo Login Page of $uid </h2>
<input id="uid" type="$cookie{uid}->value"/>
</body>
</html>
HTML_CODE
;