Normally you don't want our ajax calls returning rendered pages (ie, JSPs) and rather they should follow RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1 Here's a controller from a project I'm working on.
@Controller
public class MilestoneController extends BaseRESTController {
protected static final String ACCEPT_JSON = "Accept=application/json";
@Resource private GuidelineService _guidelineService;
@RequestMapping(value="/guideline/{id}/milestones", method= RequestMethod.GET, headers=ACCEPT_JSON)
public @ResponseBody List<Milestone> getMilestones(@PathVariable("id") Long guidelineId) {
return _guidelineService.getGuideline(guidelineId).getMilestones();
}
@RequestMapping(value="/guideline/{id}/milestones/new", method= RequestMethod.GET, headers=ACCEPT_JSON)
public @ResponseBody Milestone addMilestones(@PathVariable("id") Long guidelineId) {
return _guidelineService.newMilestone(guidelineId);
}
@RequestMapping(value="/guideline/{id}/milestones", method={RequestMethod.POST, RequestMethod.PUT}, headers=ACCEPT_JSON)
public @ResponseBody ResponseEntity<String> updateUpdateMilestone(@PathVariable("id") Long guidelineId, @RequestBody Milestone milestone) {
_guidelineService.updateMilestone(guidelineId, milestone);
return new ResponseEntity<String>(HttpStatus.ACCEPTED);
}
}
If you have jackson in your classpath using @ResponseBody will render your return value to JSON. And @RequestBody will allow you to POST and PUT json from your client. In my updateUpdateMilestone() method you can see how I don't need to return a value so i'm just returning a 202 (HttpStatus.ACCEPTED).
To use what @Sotirios posted
$.ajax({
type: "POST",
url: someUrl,
data: someData,
success: function(data){
windows.href.location = someNewLocation;
},
error: function(X) {
}
});
You controller method would need to be something like
@RequestMapping(value="/somePage", method={RequestMethod.POST},
headers="Accept=application/json")
public @ResponseBody String doStuff(@RequestBody SomeObject obj) {
// do stuff here....
return "viewName"
}
without @ResponseBody the control will try to render the view by name.