这是客户端代码:
function startAjax() {
$.ajax({
type : 'GET',
url : 'customers/ShowAllCustomers',//this is url mapping for controller
dataType: 'json',
contentType: 'application/json',
mimeType: 'application/json',
success : function(response) {
alert(response.get(0).first_name);
//this response is list of object commming from server
},
error : function() {
alert("opps error occured");
}
});
}
这是我的控制器:
@RequestMapping(value="/ShowAllCustomers", method = RequestMethod.GET)
public @ResponseBody List<Customer> AllCustomersList() {
List<Customer> customers= customerService.getAllCustomers();
return customers;
}
每次出现“oops error occurred”警告。能否请你指出这个错误。我会非常感激......
答案 0 :(得分:0)
List<Customer> customers= customerService.getAllCustomers();
return customers;
上面的代码返回一个Java List
,但是jQuery期待一个JSON字符串。
dataType: 'json',
contentType: 'application/json',
mimeType: 'application/json',
您需要使用一些JSON库将List
转换为JSON字符串。