Php jQuery验证程序远程方法
我使用的是远程验证方法,对下面的部分有疑问 响应被评估为JSON,对于有效元素,响应必须为true;对于无效元素,响应可以为false、undefined或null,使用默认消息;或字符串,例如“该名称已被采用,请改用peter123”显示为错误消息 如果服务器Php jQuery验证程序远程方法,php,jquery,jquery-validate,Php,Jquery,Jquery Validate,我使用的是远程验证方法,对下面的部分有疑问 响应被评估为JSON,对于有效元素,响应必须为true;对于无效元素,响应可以为false、undefined或null,使用默认消息;或字符串,例如“该名称已被采用,请改用peter123”显示为错误消息 如果服务器echo('customerror'),未通过验证,但不显示错误。为什么不呢 如果服务器执行echo(null)或回波(假),或者根本没有回音,客户端似乎没有收到响应,它没有通过验证,默认消息也没有显示。它不应该显示默认消息吗?类似地,如
echo('customerror')代码>,未通过验证,但不显示错误。为什么不呢
如果服务器执行echo(null)代码>或回波(假)代码>,或者根本没有回音,客户端似乎没有收到响应,它没有通过验证,默认消息也没有显示。它不应该显示默认消息吗?类似地,如果服务器执行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');
?>
如果服务器回显('custom error');,则不会通过验证,但不会显示错误。为什么不显示
因为您需要将服务器响应作为JSON字符串发送回去;不是常规字符串,也不是布尔值
echo json_encode('this is going to be the error message');
这将通过验证
echo json_encode('true');
这将导致验证失败,并使用默认消息
echo json_encode('false');
这些也将工作如上所述
echo 'true' // pass
echo 'false' // fail using default message
这行不通
echo 'custom message';
//以下结果导致客户端接收1,“1”显示为错误[snip]
文件:
响应的计算结果为JSON,对于无效元素,响应必须为任何false、undefined或null
任何未被解释为包含“true”
的字符串的内容都将无法通过验证由于您没有将这些响应作为JSON进行响应,您的各种错误消息被证明是非常不可预测的。
是的,我同意文档对JSON的措辞有点含糊不清。“评估为JSON”似乎暗示插件正在进行转换。事实并非如此。服务器响应在计算时必须是JSON
GET
已经是remote
方法的默认值,不需要指定
我创建了一个用于将文档更改为:
服务器端响应必须是JSON字符串,对于有效元素,该字符串必须是“true”
;对于无效元素,该字符串可以是“false”
、未定义
,或空
,使用默认错误消息。如果服务器端响应是一个字符串,例如,“已使用该名称,请尝试使用peter123”
,此字符串将显示为自定义错误消息,而不是默认值
由于使用remote
方法已经有很多关于混淆的问题,因此我几乎将此作为一个副本来结束。然而,你的更具体一点,也许将来可以用来关闭别人。这让我发疯了!感谢you@user1032531,我认为很多人都有问题,仅仅是因为文件中的措辞。我已建议对上述编辑中描述的文档进行更改。请随时在上对此发表评论。谢谢
echo 'custom message';
type:'get'