在代码中检查以下几点:
@Controller @RequestMapping("/user") public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String userHome(ModelMap model) {
return "user/home";
}
}
$(document).ready(function() { $.ajax({ url: 'user', type: 'get', success: function(data) { $('#content').html(data); }, error: function(xhr, status, error) { alert(xhr.responseText); } }); });
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE) public @ResponseBody String getUser(ModelMap model) { // Some logic here to fetch user details return "{'name': 'John', 'email': 'john.doe@example.com'}"; }
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE) public @ResponseBody String getUser(ModelMap model) { // Some logic here to fetch user details JSONObject user = new JSONObject(); user.put("name", "John"); user.put("email", "john.doe@example.com"); return user.toJSONString(); }
如果以上几点都已检查并确认,那么应该能够成功地返回AJAX调用的数据。