简体   繁体   中英

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: 'application/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM