0

Here is Client side code:

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");
        }
    });
}

and here is my Controller:

@RequestMapping(value="/ShowAllCustomers", method = RequestMethod.GET)
    public @ResponseBody List<Customer> AllCustomersList() {
        List<Customer> customers= customerService.getAllCustomers();
        return customers;
    }

Everytime "oops error occured" alert appears. Can you please point out error in this for me. I will be really thankful......

4

1 回答 1

0
List<Customer> customers= customerService.getAllCustomers();
return customers;

上面的代码返回一个 Java List,但 jQuery 需要一个 JSON String 。

dataType: 'json', 
contentType: 'application/json',
mimeType: 'application/json',

您需要List使用一些 JSON 库将其转换为 JSON 字符串。

于 2013-06-17T06:38:02.093 回答