I am attempting to transmit a json
object
from javascript
to the Spring controller
. My method of choice is using angularJs $http post
for this. The issue arises when I send the key
as object
, with the lastName showing up as null
. Strangely, if I send the string
as a hard-coded value, it does get picked up in the controller
. Below is the snippet of the controller
code:
@RequestMapping(value="/edit", method= RequestMethod.POST)
@ResponseBody
public void editInformation(@RequestBody UserDetails userDetails){
LOGGER.debug("THE LASTNAME IS: "+userDetails.getLastName());
//codes.....
}
Now moving on to the angularJs part:
$fieldProperty =$(this).attr("name");
$inputValue =$(this).val();
$http.post("/app/edit", {$fieldProperty : $inputValue}).success(function(result){
alert("Success "+result)
}).error(function(data, status){
//$log.info("The error is: "+data+ " and the error status code is: "+status)
alert("failure"+" and the data is: "+data+ " and the stis "+status)
});
When sending {$fieldProperty : $inputValue}
as JSON, it results in userDetails.getLastName()
returning null. However, sending {"lastName" : $inputValue}
yields the desired outcome. I even checked by using alert($fieldProperty)
which confirms lastName
.
Just a note that I am employing the Google Gson
library.
If anyone can spot what I might be overlooking here, your assistance would be greatly appreciated. Thank you.