Reputation: 143
I have a MVC Spring controller. I call this ajax on page load.
$.ajax({
type: "get",
url: 'custom/Service',
data: "note1=" + "11",
dataType: "json",
async: false,
success: function() {
alert("1");
},
error:function (xhr, ajaxOptions, thrownError){
if(xhr.status == 200 && xhr.statusText == 'parsererror'){
window.location.reload();
} else {
alert(xhr.status+","+xhr.statusText);
alert(thrownError);
}
}
});
My controller is:
@RequestMapping("/custom")
public class CustomController {
@RequestMapping(value="/Service", method=RequestMethod.GET)
public String Service(
@RequestParam("note1") String note1,
HttpServletRequest request, HttpServletResponse response, Locale locale,
Model model) {
String result = custom.Service(note1, request, response);
System.out.println("result: " + result);
return result;
}
}
The out put is correct in the console for the controller. But I get "Not Found" error. Here is the developers tool error: GET "MySite/custom/Service?note1=11" 404 (Not Found) . What is wrong?
Upvotes: 1
Views: 1304
Reputation: 47290
you need to add response body annotation to your return parameter
public @ReponseBody String Service(
A spring controller doesn't have to return a view, it can return data, with the reposnsebody annotation. Your original code will look up the result variable as a jsp according to your view resolver settings.
Upvotes: 0
Reputation: 8322
Change your code like below:
@RequestMapping(value="/Service", method=RequestMethod.GET)
public @ResponseBody String Service(
@RequestParam("note1") String note1,
HttpServletRequest request, HttpServletResponse response, Locale locale,
Model model) {
String result = custom.Service(note1, request, response);
return result;
}
You can access the result in your ajax success function.
success: function(result) {
alert(result);
},
Upvotes: 1