我目前有一个用于密码检查的 php 功能,可以在 php 上完美运行。但是,当我尝试从 AJAX 返回结果时,“+”符号不知何故未被检测到。
这是 php 功能:
function pwd_contents($pwd, $notAllowedChar)
{
$pass_array = str_split($pwd);
$notAllowedChar_array = str_split($notAllowedChar);
foreach($notAllowedChar_array as $nChar)
{
if(in_array($nChar, $pass_array))
return true;
}
return false;
}
if(isset($_POST['password']))//If a username has been submitted
{
$pattern = '/\/\@\+\=\#\*/';
$password = $_POST['password'];//Some clean up :)
if(pwd_contents($password, $pattern))
echo '1';
else
echo '0';
}
这是调用它的 ajax 调用:
$.ajax({ //Make the Ajax Request
type: "POST",
url: "ajax_check_password.php", //file name
data: 'password=' + password, //data
success: function(server_response) {
$("#password_status").ajaxComplete(function(event, request) {
if (server_response.trim() == '0') //if ajax_check_password.php return value "0"
{
$("#password_status").html('<img src="/img/icon-success.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#c9dc54" style="padding-top: 5px;"> Password is valid. </font> ');
$("#btn-submit-2").html('<button type="submit" class="btn btn-danger btn-lg btn-block" id="b" style="font-family: "klavikaRegular"; letter-spacing: 1px;" name="skip" value="0">CONTINUE</button>');
//add this image to the span with id "availability_status"
} else if (server_response.trim() == '1') //if it returns "1"
{
$("#password_status").html('<img src="/img/icon-error.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#b11116" style="padding-top: 5px;"> Password is not valid. </font>');
$("#btn-submit-2").html('<a class="btn btn-default btn-lg btn-block" style="font-family: "klavikaRegular"; letter-spacing: 1px;">CONTINUE</a>');
}
});
}
});
如果密码中包含“+”,我实际上如何获得响应以返回我想要的结果?
一个例子是“Megason+2983”。 ajax 调用的结果会给我“密码无效。”
最佳答案
因为您使用“URL 编码”方法向 AJAX POST 提供数据:
data: 'password=' + password, //data
jQuery 将密码中的“+”视为 URL 编码字符(它是 URL 中的保留字符),因此在 PHP 有机会处理该字符之前,它会被转换为其等效的明文(空格字符)。发布。
要修复它,请将其更改为:
data: {password: password}, //data
对于 future AJAX 请求的故障排除:通过浏览器中的开发人员工具检查请求可能会很快为您指出解决方案。
关于javascript - 如何使用 AJAX 和 jQuery 正确检查包含 "/+=@#^"的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26812720/