使用jQuery验证插件,我一直在尝试检索表单的输入字段值,将其与一些url前缀连接以生成REST URL,然后通过remote
方法向生成的url发出GET请求。
下面是REST接口的方法(服务器端是java)
@Path("/user")
public interface UserResource {
@GET
@Path("/registered/email/{value}")
public boolean isRegistered(@PathParam("value") String value);
}
这是要验证的输入字段的样子
<form id="registerForm" action="/register" method="POST">
<input class="form-control" id="email" name="email" type="email" value='${email}' required/>
...
</form>
然后是jQuery验证脚本;
$('body #registerForm').validate({
rules : {
'email': {
required: true,
email: true,
remote: "/user/registered/email/" + $(this).find('#email').val()
},
},
messages : {
'email': {
required: 'Please enter your email address',
email: 'Please provide a valid email address',
remote: 'Email has already been taken',
},
},
});
请注意传递给remote
方法的值只是REST网址,因为根据http://jqueryvalidation.org/remote-method/,默认请求type
是GET ...还要注意dataType
和data
是如何指定的,因为在这种情况下不一定需要它们。
目标:...现在,说用户输入的电子邮件是[email protected]
我通常会期望得到的网址如下所示;
http://localhost:8080/user/registered/email/[email protected]
问题:......但是这就是我得到的;
http://localhost:8080/user/registered/email/[email protected]
问题:...在网址中注意?email=
之前的[email protected]
...我想知道为什么$(this).find('#email').val()
的结果被串联作为查询参数?...请有人向我解释为什么会发生这种情况?还有怎么样?我能解决这个问题吗?
谢谢。
问题:...注意网址中的
?email= before [email protected]
...我想知道为什么$(this).find('#email').val()
的结果被串联作为查询参数?...请有人向我解释为什么会发生这种情况?还有我怎么做解决这个问题?
默认情况下,查询字符串?field="value"&field2="value2"....
正是数据与GET
请求一起发送的方式。通常,您不会使用URL段在服务器端获取此值,只需使用GET
数组。
The jQuery Validate plugin's remote
method使用与jQuery .ajax()
相同的选项。参考the .ajax()
documentation ......
数据 键入:PlainObject或String或Array 要发送到服务器的数据。如果不是字符串,它将转换为查询字符串。它附加到GET请求的URL。请参阅
processData
选项以防止此自动处理。对象必须是键/值对。如果value是一个数组,jQuery会根据传统设置的值使用相同的键序列化多个值(如下所述)。
...
processData(默认值:true) 类型:布尔值 默认情况下,作为对象传入数据选项的数据(技术上,不是字符串)将被处理并转换为查询字符串,适合默认内容类型“application / x-www-form-urlencoded” 。如果要发送DOMDocument或其他未处理的数据,请将此选项设置为false。
OP标题:JQuery验证 - 如何在“远程”方法中正确检索表单字段值?
要获取该字段的值,只需选择该字段并将其附加到jQuery .val()
。没有必要使用$(this).find(...
$('#email').val()
试试这个...
rules : {
'email': {
required: true,
email: true,
remote: {
url: "/user/registered/email/" + $('#email').val(),
processData: false
}
},
},
目标:...现在,说用户输入的电子邮件是[email protected]我通常希望生成的URL看起来如下所示;
http://localhost:8080/user/registered/email/[email protected]
问题:......但是这就是我得到的;
http://localhost:8080/user/registered/email/[email protected]
这里有两个问题。第一个是验证方法在执行时解析url路径 - 默认情况下不会动态地重新检查它。当它运行时,基本上在呈现表单时,您的电子邮件输入字段为空。因此,路径被解析为'http://localhost:8080/user/registered/email/'+''(您开始使用的静态字符串,与空输入字段中的空字符串连接)。
第二个问题是jQuery.validate的远程方法默认沿着验证字段的'name'和'value'属性作为查询参数发送 - 当然,这不是REST的做事方式。这些被展平为'?email = username @email.com',它放在URL的其余部分之后,因为它在第一步中被解析。
要解决第一个问题,并构建一个REST值得路径,您必须将URL构建包装在一个函数中,每次在该输入上请求验证时都会执行该函数。
$('body #registerForm').validate({
rules : {
'email': {
required: true,
email: true,
remote: function() {
return '/user/registered/email/' + $('#email').val()
}
}
}
});
这将产生一个正确的REST API URL,但它仍然会附加查询字符串。它看起来像这样:
http://localhost:8080/user/registered/email/[email protected][email protected]
当然,这很难看,但是你的API很可能会默默地忽略查询字符串,并按预期执行。
但是为了获得更清晰,更RESTful的url,您需要重置ajax调用的data属性。您可以通过让步骤1中的远程函数返回一个对象,并将之前的url字符串作为url参数,并将数据参数设置为空字符串来实现。
$('body #registerForm').validate({
rules : {
'email': {
required: true,
email: true,
remote: function() {
return {
url: '/user/registered/email/' + $('#email').val(),
data: ''
}
}
}
}
});
这将产生所需的RESTful URL
http://localhost:8080/user/registered/email/[email protected]
您正在向网站上的文件夹或位置发出获取请求。您需要提供执行检查的脚本。所以你的远程网址应该是“/user/registered/email/check_email.php”。
此外,GET请求的整个想法是参数作为名称/值对传递,在“?”之后添加到查询字符串上。分隔器。见http://www.w3schools.com/tags/ref_httpmethods.asp。您没有提供实际的远程脚本,也没有为您的值提供参数名称,因此您只需获取网址+“?” +你的价值。
最后,我建议你使用POST方法。在您提供链接的资源中,请查看第二个示例。它向您展示了如何指定POST。
这一切都有意义吗?
我使用此代码来解决REST服务问题:
$("#mailingAddress").rules("add", {
required: true,
remote: function () {
var r = {
url: "/Api/Address/ValidateAddressSimple/" + $("#mailingAddress").val(),
type: "post",
cache: false,
dataFilter: function (response)
{
return response;
}
};
return r;
},
messages: {
remote: "* not valid"
}
});