在cgi和Javascript/Jquery之间传输数据

Transfer Data between cgi and Javascript/Jquery

本文关键字:之间 传输 数据 Jquery cgi Javascript      更新时间:2023-09-26

我想在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)
        }
     });
}
 }
)

选项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}->value分配给一个隐藏的输入,并让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
;
$(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)
        }
     });
}
 }
)