一个新的问题,从这个线程出现了: jQuery的表单验证从来没有让Ajax调用,在从Ajax请求返回的数据是准确的,但它总是失败的一个比较。这是code,使Ajax请求:
A new problem emerged from this thread: jquery form validator never makes ajax call, in that the data returned from the ajax request is accurate, but it always fails on a comparison. This is the code that makes the ajax request:
var pass_form = $('#pass_form');
pass_form.submit( valid_pass_sett );
function valid_pass_sett() {
//remove old errors - snipped
pass_old = $('input[name=pass_old]').val();
pass_new = $('input[name=pass_new]').val();
pass_confirm_new = $('input[name=pass_confirm_new]').val();
if (pass_old === "") {
//display error on form - snipped
return false;
} else if (pass_new === "") {
//display error on form - snipped
return false;
} else if (pass_new != pass_confirm_new) {
//display error on form - snipped
return false;
} else if (pass_new.length < 8) {
//display error on form - snipped
return false;
} else {
$.post("http://www.example.com/ajax/validate.php",{ // async validation
type: 'valid_old_change_pass',
pass_old: pass_old,
pass_new: pass_new
}, valid_pass_combo_callback);
alert('after the ajax call...');
}
return false; // cancel form submission
}
这是该请求被提交到code:
this is the code that the request is submitted to:
$username = $_SESSION['username'];
$pass_old = $_POST['pass_old'];
$pass_new = $_POST['pass_new'];
if (empty($pass_old) || empty($pass_new)) {
echo "invalid";
} else if (!User::valid_user_pass($username, $pass_old)) {
echo "invalid_old";
} else if (!Sanitize::is_legal_password($pass_new)) {
echo "invalid_new";
} else {
echo "valid";
}
和这是回调函数处理它;回调函数是一个地方比较总是失败。
and this is the callback function that processes it; the callback function is the one where the comparison always fails.
function valid_pass_combo_callback( data ) {
//breakpoint is set here
if (data == 'valid') {
//only if the form is valid!
pass_form[0].unbind('submit').submit();
}
else if (data == "invalid_old") {
//display error on form - snipped
}
else if (data == "invalid_new") {
//display error on form - snipped
}
else {
//it always jumps to here..., even though data *is* the correct value
}
}
我调试这个code和validate.php将返回invalid_old
这是正确的(根据测试数据,我进入)。因此,数据
是invalid_old
根据Firebug的存储;然而,code的总是的跳转到最后else语句。为什么总是比较失败?
I debugged this code, and validate.php is returning "invalid_old"
which is correct (based on the test data I'm entering). So, data
is storing "invalid_old"
according to Firebug; however, the code always jumps down to the last else statement. Why is the comparison always failing?
正如在注释中,加入jQuery的修剪
功能解决了这一问题,如下所示:
As stated in the comment, adding JQuery's trim
function fixed the problem, as shown:
function valid_pass_combo_callback( data ) {
trimmed_data = $.trim(data);
if (trimmed_data == 'valid') {
//only if the form is valid!
pass_form[0].unbind('submit').submit();
}
else if (trimmed_data == "invalid_old") {
//display error on form - snipped
}
else if (trimmed_data == "invalid_new") {
//display error on form - snipped
}
else {
//it always jumps to here..., even though data *is* the correct value
}
}