【问题标题】:want to pass a variable from jquery with ajax to perl file and retrieve the value in the cgi page想通过 ajax 从 jquery 传递一个变量到 perl 文件并检索 cgi 页面中的值
【发布时间】:2013-06-26 04:00:37
【问题描述】:

以下是我的html代码:

following is my template code : 

<html>
<head>

<script type = "text/javascript" src = "jquery.min.js"></script>
<script>
var counter = 0;
var txt1 = "group_save";
function addNew() {
// Get the main Div in which all the other divs will be added
var mainContainer = document.getElementById('mainContainer');
// Create a new div for holding text and button input elements
var newDiv = document.createElement('div');
// Create a new text input
var newText = document.createElement('input');
newText.type = "input";
newText.name = txt1+counter;
//newText.value = counter;
// Create a new button input
var newDelButton = document.createElement('input');
newDelButton.type = "button";
newDelButton.value = "Delete";
// Append new text input to the newDiv
newDiv.appendChild(newText);
// Append new button input to the newDiv
newDiv.appendChild(newDelButton);
// Append newDiv input to the mainContainer div
mainContainer.appendChild(newDiv);
counter++;
// Add a handler to button for deleting the newDiv from the mainContainer
newDelButton.onclick = function() {
mainContainer.removeChild(newDiv);
}
}
   function edit(element){
  var tr = jQuery(element).parent().parent();
    if(!tr.hasClass("editing")) {
            tr.addClass("editing");
            tr.find("DIV.td").each(function(){
                    if(!jQuery(this).hasClass("action")){
                            var value = jQuery(this).text();

                            jQuery(this).text("");
                            jQuery(this).append('<input type="text" value="'+value+'" />');

                    } else {
                            jQuery(this).find("BUTTON").text("save");
                    }
            });
    } else {
            tr.removeClass("editing");
            tr.find("DIV.td").each(function(){
                    if(!jQuery(this).hasClass("action")){
                            var value1 = jQuery(this).find("INPUT").val();
                            alert(value1);
                            jQuery(this).text(value1);
                            jQuery(this).find("INPUT").remove();
                    } else {
                            jQuery(this).find("BUTTON").text("edit");
                    }
            });
    }
}
</script>

</head>
<body >
<form name="group" method="post" action="process.cgi">
<div id="mainContainer">
<div><input type="button" value="Add" onClick="addNew()"></div>
 </div>
 <div><input type = "submit" value = "Save"></div>
</form>
[% IF count > 0%]

<b>Details of Groups</b><br>

<div class= "table">
<div class = "thead">
  <div class = "tr">

<div class = "td">ID</div>
<div class = "td">GROUP NAME</div>
<div class = "td">GROUP DESCRIPTION</div>
<div class = "td">IS ACTIVE</div>
<div class = "td"></div>
 </div>
</div>

<div class= "tbody">

[%- SET i = 0;
 WHILE i < id.size; -%]

 <form  class = "tr">
<div class = "td">&nbsp; [% id.$i %]<br/></div>
<div class = "td">&nbsp; [% group_name.$i %]<br/></div>
<div class = "td">&nbsp; [% group_desc.$i %]<br/></div>
<div class = "td">[% actv.$i %]<br/></div>
<div class = "td action" ><button type="button" onclick="edit(this);">edit</button>   </div>
<form>
[%-     SET i = i + 1;
END -%]
</div>
 </body>
</html>

我想编辑表格的内容,并将其传递给 cgi 文件。 我已经提醒 value1 并发现,我编辑的值将在 value1 中。 我的要求是,我需要将这些值传递给 cgi 文件。

我在该行下面写了一个这样的 ajax 调用, var value1 = jQuery(this).find("INPUT").val();

ajax 调用:

                  $.ajax({
                                    type: "GET",
                                    url : "process.cgi",
                                    data : {
                                            'value' : value1,
                                    },
                                    success : function(data) {
                                            alert("success");
                                    }

                            });

在我的 cgi 文件中,我尝试打印类似的值,

 print $cgi->param("value");

但是,那里没有打印任何值。可能是什么问题?谁能帮我解决这个问题???

【问题讨论】:

  • 如果您发送像'value' : 123 这样的静态内容会发生什么?
  • 还是没来
  • 我之前没有使用过 ajax...我在正确的位置使用了 ajax。我对此有疑问吗?请检查一下吗?
  • 这里有一个不相关的提示:使用 Firebug 并使用 Console.log(foo) 进行调试输出,这比一直发出警报要方便得多。
  • 另外,检查它是否调用了正确的脚本。尝试将warn 或carp 放入您的脚本并查看/var/log/apache2/messages 或您的Web 服务器的类似错误日志。我感觉可能缺少cgi-bin。尝试这个。你的process.cgi 是可执行文件吗? chmod 775。

标签: jquery html ajax perl cgi


【解决方案1】:

这里有许多不同的技术相互作用。最好每次测试一个,看看问题出在哪里。

  1. 从命令行运行您的 Perl 程序以确保它 做正确的事。
  2. 通过在浏览器中直接键入 URL 来运行您的 Perl 程序,以确保它正确响应 HTTP 请求。
  3. 伪造一个调用您的 Perl 程序的静态 HTML 页面,以确保您以正确的格式传递正确的参数。
  4. 将 jQuery 和 Ajax 添加到您的 HTML 页面中,以便准确地重新创建 您在第 3 阶段使用的静态表单。

在您确定每个阶段都正常工作之前,不要进入下一个阶段。只有非常糟糕的程序员才会尝试同时调试所有这些技术。

【讨论】:

  • 我以同样的方式完成了。我可以将此模板文件中的其他值传递到我的 cgi 页面。它存储在数据库中。出于更新目的,我添加了 ajax 和 jquery.and这是唯一不通过的事情
  • 我已经使用萤火虫进行了检查,发现这些值正在传递到 process.cgi 文件,但是它没有在那里打印。我认为我以前打印的格式不正确。能否请你帮忙??是否有任何用途 CGI:Ajax; cgi 页面中从 ajax 调用中获取值所需的指令
  • 您的 Perl 程序对 Ajax 一无所知。它只是一个响应 HTTP 请求的 HTTP 服务器。只要您向其发送格式正确的 HTTP 请求,它就应该可以工作。为什么不改变你的 Perl 程序来转储它接收到的所有参数。这样你就可以看到发生了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多