ajax cgi jquery,javascript - Pass data to CGI script and back with jQuery.ajax - Stack Overflow

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.

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

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".

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.

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

This is my code:

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

Enter information

First name

Name

Login

The JavaScript Code:

$(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:

#!/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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值