JQuery/Spring: How to POST JSON but return html
When I post a JSON to the server and return a normal View string from the Spring controller, my jQuery is not hitting the "success" function.
My Controller:
@RequestMapping(value = MappingConstants.RULE_ASSIGNMENT, method = RequestMethod.POST)
public String saveRuleAssignment(@RequestBody RuleAssignmentCO ruleAssignment) {
// Some controller logic ...
return "redirect:/some/view";
}
As you can see, my controller is simply taking in a JSON object, and returning a String view which is supposed to be parsed by Spring. In my logs, I can see that the view is indeed being hit, but my jQuery post method is not hitting the "success" (nor the "error") function.
My jQuery:
jQuery.ajax({
url: 'ruleAssignment',
type: 'POST',
cache: false,
dataType: 'html',
data: test,
contentType: 'appl开发者_Python百科ication/json',
error: function() {
console.log('error');
},
success: function() {
console.log('success');
}
});
If I perform a redirect with Spring (prepending "redirect:" before my View), the success method within the javascript ajax call will never get hit.
Usually I redirect after a "post" so I don't have to worry about the user hitting Refresh, but since it's an Ajax call it's unnecessary.
Thanks Gregg for the answer.
精彩评论