I need to submit data from a form and bind it to a model. The form is hidden from view and was sent via javascript.
The problem is everything gets passed to the controller but NOT THE VALUE FOR PASSWORD field.
script.js
function submitForm(username, password) {
var form = $("<form action='/some-url' method='post' id='tempForm' modelAttribute='userform'>" +
" <input type='hidden' name='username' id='username' value='" + username + "'/>" +
" <input type='hidden' name='password' id='password' value='" + password + "'/>" +
"</form>";
$('body').append(form);
form.submit();
}
Assuming we have username and password as string variables.
Here's the UserForm class
Userform.java
@Table("users")
public class UserForm{
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
// getters and setters here...
}
and the controller that is supposed to accept the submitted form.
Controller.java
@RequestMapping(value = "/some-url", method=RequestMethod.POST)
public String submitForm(@ModelAttribute("userform") UserForm userForm, BindingResult result){
// ....
// some code here
// ....
}
There are still additional fields but everything is just fine. The password column is somehow set to null when the controller is called.
How can I preserve and send the value of the password field?
Please advise.
Thanks!
:: EDIT ::
The username and password values are taken from an AJAX call. The response is shown below:
{"data":{"username":"TEST_USERNAME@EMAIL.COM","password":"iampassword"},"invalid":false,"error":null}
username and password is then passed to the submitForm(username, password) function in the JS script.
submitForm(response.data.username,response.data.password);
Meaning, the controller did not receive the values through AJAX, but through a SUBMIT FORM containing the values from the previous call.
I'll also include a screenshot in the IDE of the values when it is already passed to the controller.
Aucun commentaire:
Enregistrer un commentaire