【问题标题】:Transfer Data between cgi and Javascript/Jquery在 cgi 和 Javascript/Jquery 之间传输数据
【发布时间】:2014-09-05 14:56:24
【问题描述】:

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

 }
)

【问题讨论】:

  • var uid = $('#uid').attr('value');应该做什么? (在您的 html 上下文中的意思)。
  • 我刚学javascript,你能告诉我如何在index.cgi和test_javascript.js之间传输数据

标签: javascript jquery html perl cgi


【解决方案1】:

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}->value 分配给隐藏的输入并让 testjavascript 稍后将其拾取

索引.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
;

【讨论】:

  • 我在 javascript 中使用此警报只是为了获取 uid 的值。我有一个确定 uid 是否为管理员的 javascript。我可以从 document.search 获取 uid,但不想使用 document.search,因为它会在 url 中显示 uid。所以我从 cgi 中的表单中获得了 uid 的值。我只想在 javascript 中获取该 uid 值,以便我可以比较 uid 并判断它是否是 test_javascript.js 中的管理员
  • 所以基本上你是说你不想在你的页面源上显示 uid ?这就是为什么你试图在运行时将它传递给你的 JS?那么答案是否定的。不容易。您可以做的是让您的 cgi 生成一个动态 JS 脚本,在其中插入 UID(通过写入文本文件),然后从您的 CGI 页面引用它。
  • 我可以隐藏 uid 但在 test_javascript.js 中我想从 cgi 获取 uid 的值作为 var uid = demo 或其他东西......我认为它可以通过简单的方式完成javascript 但我对 javascript 太陌生了,我想不出任何进程
  • 使用 cookie 有其缺点。禁用 cookie 的浏览器会导致您的页面出现问题。另外,您知道 cookie 信息可以像您的 hmtl 源中的数据一样简单地查看吗?
【解决方案2】:
$(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)
        }
     });
}

 }
)

【讨论】:

  • 实际上我想在 test_javascript 中获取 index.cgi 中的 uid 值,以便我可以将其用于其他人
  • 在这种情况下,只需在 JS 而不是 Perl 中读取 cookie。我改变了答案。
  • 我收到 Uncaught TypeError: Undefined is not a function over var uid = $.cookie("uid");
猜你喜欢
  • 1970-01-01
  • 2015-09-23
  • 2014-10-07
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多