我正在尝试使用 MySQL、Perl 和 JS 为我的网站制作一个简单的注册表单。当直接通过 cmdline 或 URL 给定参数并且 MySQL 正确注册用户时,Perl 脚本工作正常。问题在于 JS;当我通过 JSON 帖子发送数据时,Perl 脚本接收该帖子并尝试创建通过代码给出的参数的哈希:
%params = $cgh->Vars()
但是,这并没有正确拆分参数并将它们分配给各个键。相反,Perl 脚本会创建一个如下所示的哈希:
%参数数据
$VAR1 = 'POSTDATA';
$VAR2 = 'action=register&uname=d&pass=d&first=d&last=d&email=anemail@themailplace.com&phone=d';
我怀疑 JS 需要改变它提交数据的方式,而不是 Perl 脚本接受数据的方式。无论如何,这里是 JS:
$(document).ready(function(){
$("form#register-form").submit(function() { // register-form is submitted
var username = $('#username').attr('value'); // get username
var password = $('#password').attr('value'); // get password
var repassword = $('#repassword').attr('value'); // get password
var firstName = $('#firstName').attr('value'); // get password
var lastName = $('#lastName').attr('value'); // get password
var email = $('#email').attr('value'); // get password
var phone = $('#phone').attr('value'); // get password
if (username && password && repassword && firstName && lastName && email && phone) { // values are not empty
$.ajax({
type: "POST",
url: "/cgi/login.pl", // URL of the Perl script
contentType: "application/json; charset=utf-8",
dataType: "json",
// send username and password as parameters to the Perl script
data: "action=register" + "&uname=" + username + "&pass=" + password + "&first=" + firstName + "&last=" + lastName
+ "&email=" + email + "&phone=" + phone,
// script call was *not* successful
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('div#register-result').text("responseText: " + XMLHttpRequest.responseText
+ ", textStatus: " + textStatus
+ ", errorThrown: " + errorThrown);
$('div#register-result').addClass("error");
}, // error
// script call was successful
// data contains the JSON values returned by the Perl script
success: function(data){
if (data.error) { // script returned error
$('div#register-result').text("data.error: " + data.error);
$('div#register-result').addClass("error");
} // if
else { // login was successful
$('form#register-form').hide();
// $('div#register-result').text("data.success: " + data.success + ", data.userid: " + data.userid);
// $('div#register-result').addClass("success");
} //else
} // success
}); // ajax
} // if
else {
$('div#register-result').text("enter username and password");
$('div#register-result').addClass("error");
} // else
// $('div#register-result').fadeIn();
return false;
});
});
除非与问题相关,否则请忽略我对表单提交的信息几乎没有检查的事实。如果您需要更多信息,请告诉我。提前谢谢你。