【发布时间】:2015-04-21 14:35:17
【问题描述】:
我正在使用远程验证方法,对http://jqueryvalidation.org/remote-method/的以下部分有疑问。
响应被评估为 JSON 并且对于有效元素必须为 true,对于无效元素可以是任何 false、undefined 或 null,使用默认消息;或字符串,例如。 “该名称已被占用,请尝试 peter123”以显示为错误消息。
如果服务器echo('custom error');,则不通过验证,但不显示错误。为什么不呢?
如果服务器执行echo(null); 或echo(false);,或者根本没有回显,则客户端似乎没有得到响应,它没有通过验证,并且默认消息不是显示。它不应该显示默认消息吗?同样,如果服务器执行echo('undefined');,客户端会收到“未定义”但不显示默认消息。
服务器脚本
<?php
header('Content-Type: text/plain;');
//The following passes validation
//echo('true');
//The following results in the client receiving 1, and "1" is displayed as error
//echo(true);
//echo(1);
//echo('1');
//The following will trigger the default message
//echo(0);
//echo('null');
//echo('false');
//echo('0');
//The following results in no ajax response to client, and no message is displayed.
//Doesn't this result in client getting undefined which should display the default message
//echo(null);
//echo(false);
//no echo at all
//Client receives "undefined", but it doesn't display the default message. Shouldn't it?
//echo('undefined');
//Client receives "custom error", but it doesn't display this text. Shouldn't it?
echo('custom error');
?>
客户端脚本
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var validator=$("#myForm").validate({
rules: {
bla: {
minlength:2,
maxlength:4,
required:true,
remote: {url:"validate.php",type:'get',data:{a:1,b:2,c:3}}
//remote: "validate.php"
}
},
messages: {
bla: {
remote:"default message"
}
}
});
});
</script>
</head>
<body>
<form id="myForm" method="post">
<input name="bla" id="bla" value="">
<input type="submit" value="submit">
</form>
</body>
</html>
【问题讨论】:
-
由于已经有很多关于使用
remote方法混淆的问题,我几乎将其作为 this 的副本关闭。但是,您的更具体一点,也许将来可以用来关闭其他人。
标签: php jquery jquery-validate