[英]Perl CGI->Vars() not properly hashing cgi parameters
I am trying to use MySQL, Perl and JS to make a simple registration form for my website.我正在尝试使用 MySQL、Perl 和 JS 为我的网站制作一个简单的注册表单。 The Perl script works fine when given parameters directly via cmdline or via the URL and the MySQL properly registers the user.
当直接通过 cmdline 或通过 URL 给定参数并且 MySQL 正确注册用户时,Perl 脚本工作正常。 The issue is with the JS;
问题出在 JS 上; when I sent the data via a JSON post the Perl script receives the post and attempts to create a hash of the parameters given via the code:
当我通过 JSON 帖子发送数据时,Perl 脚本接收帖子并尝试创建通过代码给出的参数的散列:
%params = $cgh->Vars()
However, this doesn't properly split the parameters and assign them to individual keys.但是,这不能正确拆分参数并将它们分配给各个键。 Instead, the Perl script creates a hash that looks like this:
相反,Perl 脚本会创建一个如下所示的散列:
%params data %params 数据
$VAR1 = 'POSTDATA';
$VAR2 = 'action=register&uname=d&pass=d&first=d&last=d&email=anemail@themailplace.com&phone=d';
I suspect that the JS needs to change how it is submitting the data and not the way the Perl script is accepting the data.我怀疑 JS 需要改变它提交数据的方式,而不是 Perl 脚本接受数据的方式。 In any case, here is the JS:
无论如何,这是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;
});
});
Unless relevant to the issue, please ignore the fact that I do next to no checks on the info submitted by the form.除非与问题相关,否则请忽略我几乎不检查表单提交的信息这一事实。 Let me know if you require any more information.
如果您需要更多信息,请告诉我。 Thank you in advanced.
先谢谢了。
You are not sending JSON to server so using contentType: "application/json; charset=utf-8",
will over ride default 'application/x-www-form-urlencoded; charset=UTF-8'
您没有将 JSON 发送到服务器,因此使用
contentType: "application/json; charset=utf-8",
将超过默认的'application/x-www-form-urlencoded; charset=UTF-8'
'application/x-www-form-urlencoded; charset=UTF-8'
Using the default, server script deals with the key/value pairs as it would for any other form submission使用默认值,服务器脚本像处理任何其他表单提交一样处理键/值对
Per the CGI documentation:根据 CGI 文档:
If POSTed data is not of type application/x-www-form-urlencoded or multipart/form-data, then the POSTed data will not be processed, but instead be returned as-is in a parameter named POSTDATA.
如果 POSTed 数据不是 application/x-www-form-urlencoded 或 multipart/form-data 类型,则不会处理 POSTed 数据,而是在名为 POSTDATA 的参数中按原样返回。
So remove your content type specification and allow jQuery to use its default application/x-www-form-urlencoded
.所以删除你的内容类型规范并允许 jQuery 使用它的默认
application/x-www-form-urlencoded
。 ( multipart/form-data
is used for form data that includes file uploads.) (
multipart/form-data
用于包含文件上传的表单数据。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.