0

I am trying to POST to a Spring MVC controller method via ajax. Using Chrome's developer tools, I can tell that the values from the form are being sent, but in the method, the form values are null.

Here is the jquery call:

var form = $('#renegotiationForm').serialize();
$.ajax({

    method:'POST', 
    url:'/renegotiate/wizard/startRenegotiation', 
    data:{'renegotiationForm': form}, 
    success: function(data) { this.transitionTo(data); }

});

Here is the Spring MVC method (meant to return only a single string):

@RequestMapping(value="wizard/startRenegotiation", method = RequestMethod.POST)
public @ResponseBody String processStart(@ModelAttribute("renegotiationForm") RenegotiationForm form, BindingResult bindingResult) {

log.debug("Entered showStart(), method=POST");

RenegotiationType type = RenegotiationType.valueOf(form.getRenoType().trim().toUpperCase());
RenegotiationActivity activity = RenegotiationActivity.valueOf(form.getRenoActivity().trim().toUpperCase());

String result = "";
if (type == RenegotiationType.TYPE1 && activity == RenegotiationActivity.ACTIVITY1) {
    result = "deleteType1";
}

return result;
}

The values are bound using the Spring Form taglib, and I have confirmed that the path attributes of the form tags match the fields of the RenegotiationForm.

2 Answers 2

1

I think it's because you are trying to send an "string" from ajax and you want to get and Object (RenegotiationForm), try to change it to String and Format in Server-side. I recommend you to add the type you are sending from client-side, too.

@RequestMapping(value = "wizard/startRenegotiation", method = RequestMethod.POST, produces="application/json")
Sign up to request clarification or add additional context in comments.

2 Comments

In this case, I only want a single, plain String as the response. This string is used to determine a transition for a Machina.js state machine. So, the value "deleteType1" will determine the next state. However, the code never makes it that far anyways.
The problem is how you get the request... @ModelAttribute("renegotiationForm") RenegotiationForm form try changing "RenegotiationForm" to "String" ... @ModelAttribute("renegotiationForm") String form
0

Found the answer. Further on in my code, I had a function like this:

var ajaxcall = function() { $.ajax({
     // ajax settings
    });
}

Unfortunately, setting it up this way doesn't make it work as a jquery deferred, and specifically I couldn't use the .then() function to process of ajax requests.

Thanks for the help.

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.