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......

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
Khurram Ilyas
  • 117
  • 3
  • 19

1 Answers1

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

The above code returns a Java List , but the jQuery is expecting a JSON String .

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

You need to convert the List to a JSON string using some JSON library.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • See [this](http://stackoverflow.com/questions/14228912/how-to-convert-list-to-json-in-java) . – AllTooSir Jun 17 '13 at 06:44
  • I have changed by controller, its now looks like this: @RequestMapping(value="/ShowAllCustomers", method = RequestMethod.GET) public @ResponseBody String AllCustomersList() { List customers= customerService.getAllCustomers(); String json_customers = new Gson().toJson(customers); return json_customers; } but still not producing desired results; – Khurram Ilyas Jun 17 '13 at 07:05
  • consol says: at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68) at – Khurram Ilyas Jun 17 '13 at 07:10
  • actually there is no stacktrace. when I give the specified URL then consol gives this message and nothing happends any more. – Khurram Ilyas Jun 17 '13 at 07:13
  • sorry sir. but I can't found server log :-( – Khurram Ilyas Jun 17 '13 at 07:23
  • I found Server Log. It says: java.lang.StackOverflowError at java.lang.StringBuffer.append(Unknown Source) at java.io.StringWriter.write(Unknown Source) at com.google.gson.stream.JsonWriter.string(JsonWriter.java:519) at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:387) at com.google.gson.stream.JsonWriter.beginArray(JsonWriter.java:280) – Khurram Ilyas Jun 17 '13 at 07:50