0

I am trying to send json objectfrom javascript to the Spring controller. I am using angularJs $http post for that. I am getting the lastName as null when I send the key as obejct. However, when I send the string as hard-coded value, the value shows up in the controller. Here is the controller code:

@RequestMapping(value="/edit", method= RequestMethod.POST)
@ResponseBody
    public void editInformation(@RequestBody UserDetails userDetails){
        LOGGER.debug("THE LASTNAME IS: "+userDetails.getLastName());
        //codes.....

    }

Here is the angularJs code:

$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)
            });

Sending {$fieldProperty : $inputValue} as JSON returns userDetails.getLastName() as null. However, sending {"lastName" : $inputValue} returns the proper value. I checked with alert($fieldProperty) and it returns lastName though. I am using Google Gson library.

Is there something I am missing here. I would appreciate your help. Thank you.

1 Answer 1

1

To set $fieldProperty u have to define the object first. This declaration ({$fieldProperty : $inputValue}) adds the key '$fieldProperty' to the Json Obj. Thats why you are getting null for lastName in Spring Controller. U can use this way

$fieldProperty =$(this).attr("name");
$inputValue =$(this).val();
var params = {};
params[$fieldProperty] = $inputValue;
$http.post("/app/edit", params).success(function () {
      // Success Callback
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.