jQuery远程验证传递额外的参数? [英] JQuery remote validation passing extra parameter?
问题描述
我的后端验证仅接受1个参数:email
My back-end validation only accepts 1 parameter: email
当我查看Firebug时,可以看到请求的URL发送了2个参数:
When I look at Firebug, I can see that the URL of the request sends 2 parameters:
https://example.com/rest/checkDupEmail?newEmail=myEmail%40myEmail.com&email=
这是验证码...
HTML :
<input type="textbox" name="newEmail" id="newEmail"/>
JS :
validator = $('#emailForm').validate({
rules: {
newEmail: {
required: true,
remote: {
url: '/rest/checkDupEmail',
data: { email: $('#newEmail').val()},
dataFilter: function(data) {
var json = JSON.parse(data);
console.log($('#newEmail').val());
console.log(data);
}
}
}
}
});
就像将我指定的HTML字段(newEmail)作为参数发送给它一样?
It's like its taking the HTML field I specify (newEmail) and sending it as a parameter?
推荐答案
引用OP :
就像它采用我指定的HTML字段(newEmail)并将其作为参数发送吗?"
是的,当然.这是remote
方法的默认行为...它从要求值的字段发送数据.
Yes, of course. That's the default behavior of the remote
method... it sends the data from the field being evaluated.
它正在发送两个数据参数,因为那是您设置它的方式.
It's sending two data parameters because that's how you've set it up.
-
它已经发送了
newEmail
字段的值,因为这是您为remote
验证选择的字段.这是使用remote
方法时的默认行为.
It's already sending the
newEmail
field's value because that's the field you've selected forremote
validation. This is the default behavior when using theremote
method.
它还会再次发送与email
相同的值,因为这正是您使用data
选项定义它的方式.
It's also sending the same value again as email
because that's exactly how you've defined it with your data
option.
data: { email: $('#newEmail').val() },
通常,仅当您要发送 其他数据以及默认数据时,才使用data
选项.在这种情况下,要评估的字段的值newEmail
是默认数据.
Typically, the data
option is only used when you want to send additional data along with the default data. In this case, the value of the field being evaluated, newEmail
, is the default data.
您应该完全删除data
选项,并且仅在后端接受newEmail
.即使没有JavaScript或jQuery Validate插件,newEmail
仍将是提交后自然将这些数据自然传递到服务器的方式.
You should remove the data
option entirely and only accept newEmail
on your back end. Even without JavaScript or the jQuery Validate plugin, newEmail
would be exactly how this data naturally comes through to the server upon submit.
完全删除data
选项,然后将标记重命名为email
或修复服务器端代码以接受newEmail
...不要不发送两次相同的数据.
Remove the data
option entirely, then either rename your markup to email
or fix the server-side code to accept newEmail
... do not send the same data twice.
文档: http://jqueryvalidation.org/remote-method/
这篇关于jQuery远程验证传递额外的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!