How to get PUT request data using Java Play Framework 2.0.3 and easyXDM
-
12-12-2019 - |
Question
I am trying to call my own web service (Java Play 2.0.3) from a web application using easyXDM
this is my code on the client side (web):
var formValues = {
email: $('#editUserEmail').val(),
first_name: $('#editUserFirstName').val(),
last_name: $('#editUserLastName').val()
};
this.xhr.request({
url: "http://restserver.dev:9000/users/" + this.model.get('id'),
method: "PUT",
data: formValues
}, function(response){
console.log(response.data);
var jsonResponse = JSON.parse(response.data);
if (jsonResponse.status == 'success'){
alert("saved successfully");
}
});
Here is code for the server (Java) :
final Map<String, String[]> values = request().body()
.asFormUrlEncoded();
// validating
if (values.containsKey("first_name"))
db_user.first_name = values.get("first_name")[0];
if (values.containsKey("last_name"))
db_user.last_name = values.get("last_name")[0];
if (values.containsKey("email"))
db_user.email = values.get("email")[0];
It works fine for POST method, but whenever I tried using PUT method, somehow the server can't get the PUT request body data (values is null). I change it to POST with the same code and it works fine. Did I do something wrong on the client or server side?
Thanks!
Solution
As far as I remember not all browsers support PUT on Ajax requests. This may the reason you are having an issue here. For cross-browser compatibility, you should stick to GET/POST on Ajax calls.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow