Using jQuery validation plugin, I have been trying to retrieve a form's input field value, concatenate it with some url prefix to make a REST url, and then make a GET request to the resulting url via remote
method.
Below's the REST interface's method (server side is java)
@Path("/user")
public interface UserResource {
@GET
@Path("/registered/email/{value}")
public boolean isRegistered(@PathParam("value") String value);
}
And here's what the input field to be validated looks like
<form id="registerForm" action="/register" method="POST">
<input class="form-control" id="email" name="email" type="email" value='${email}' required/>
...
</form>
And then the jQuery validation script;
$('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',
},
},
});
Please note how the value passed to the remote
method is simply the REST url because according to http://jqueryvalidation.org/remote-method/, the default request type
is GET... Note also how dataType
and data
are not specified because they are not necessarily needed for this case.
THE GOAL:
... Now, say the email entered by the user is username@email.com
I would normally expect the resulting url to look like the following;
http://localhost:8080/user/registered/email/username@email.com
THE PROBLEM: ... but instead here's what I get;
http://localhost:8080/user/registered/email/?email=username@email.com
QUESTION: ... Notice the ?email=
before username@email.com
in the url... I wonder why the result of $(this).find('#email').val()
is being concatenated as a query param?... Please can somebody explain to me why this is happening?.. And also how do I solve this problem?
Thanks.