通过jQuery.ajax将数据传递给CGI脚本并返回

时间:2022-05-24 12:13:05

I'm working with jQuery.ajax() to send some HTML form data to my Perl script on the server and then return some data back to the frontend.

我正在使用jQuery.ajax()将一些HTML表单数据发送到服务器上的Perl脚本,然后将一些数据返回到前端。

Preferably, the data should be in text/string format. I also need to save it as a variable for further use in jQuery functions.

最好数据应该是文本/字符串格式。我还需要将它保存为一个变量,以便在jQuery函数中进一步使用。

I already set up the HTML code, the JavaScript code and the CGI Perl script, but while JavaScript is working, the connection to the CGI Perl script is not and I always get the error message: "script call was not successful".

我已经设置了HTML代码、JavaScript代码和CGI Perl脚本,但是虽然JavaScript在工作,但是与CGI Perl脚本的连接却没有,我总是得到错误消息:“脚本调用不成功”。

Maybe some one can tell me where the mistake is? I'm not completely sure how to return the variable from the Perl script back to the server.

也许有人能告诉我错误在哪里?我不完全确定如何将该变量从Perl脚本返回到服务器。

Since I'm still new to both jQuery and JavaScript and haven't worked with asynchronous calls before, any help would be greatly appreciated.

由于我还是jQuery和JavaScript的新手,以前没有使用过异步调用,所以非常感谢您的帮助。

This is my code:

这是我的代码:

The HTML code: (where the user fills the form with his first name and name:

HTML代码:(用户在表单中填上他的名字和名字:

<html>
  <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="get_names.js"></script>
  </head>
  <body>
    <div id="loginContent" class="container">
        <div id="loginResult" style="display:none;">
        </div>
        <form id="loginForm" name="loginForm" method="post" action="">
        <fieldset>
            <legend>Enter information</legend>
            <p>
            <label for="firstname">First name</label>
            <br />
            <input type="text" id="firstname" name="firstname" class="text" size="20" />
            </p>
            <p>
            <label for="name">Name</label>
            <br />
            <input type="test" id="name" name="name" class="text" size="20" />
            </p>
            <p>
            <button type="submit" class="button positive">
             Login
            </button>
            </p>
        </fieldset>
        </form>
    </div>
  </body>
</html>

The JavaScript Code:

JavaScript代码:

$(document).ready(function(){
  $("form#loginForm").submit(function() { // loginForm is submitted


var firstname = $('#firstname').attr('value'); // get first name
var name = $('#name').attr('value'); // get name

if (firstname && name) { // values are not empty
    $.ajax({
        type: "POST",
        url: "/cgi-bin/perl_script.cgi", // URL of the Perl script

        // send firstname and name as parameters to the Perl script
        data: "firstname=" + firstname + "&name=" + name,

        // script call was *not* successful
        error: function() { 
            alert("script call was not successful");
        }, 

        // script call was successful 
        // perl_data should contain the string returned by the Perl script 
        success: function(perl_data){
            alert("Your name is: " + perl_data)
        }
    });   
}

else {
  $('div#loginResult').text("Enter your name");
  $('div#loginResult').addClass("error");
}

$('div#loginResult').fadeIn();
return false;
  });
});

The Perl code:

Perl代码:

#!/usr/bin/perl -w
use CGI;
use strict;
use warnings;

# read the CGI params
my $cgi = CGI->new;
my $firstname = $cgi->param("firstname");
my $name = $cgi->param("name");

my $completename = $firstname . $name;

print $completename;

1 个解决方案

#1


7  

The problem is in the Perl program. It does not compile. It does not send the required HTTP headers. Add print $cgi->header('text/plain;charset=UTF-8'); or similar and take care to encode the output appropriate for the content type.

问题在Perl程序中。它没有编译。它不发送所需的HTTP头。添加打印$ cgi - >标题(“text /平原;charset = utf - 8”);或类似的,请小心为内容类型的输出进行编码。

#1


7  

The problem is in the Perl program. It does not compile. It does not send the required HTTP headers. Add print $cgi->header('text/plain;charset=UTF-8'); or similar and take care to encode the output appropriate for the content type.

问题在Perl程序中。它没有编译。它不发送所需的HTTP头。添加打印$ cgi - >标题(“text /平原;charset = utf - 8”);或类似的,请小心为内容类型的输出进行编码。