0

Simply, I'm trying to parse a List of composite objects passed from Spring controller via ModelAndView object as the following

Spring part

ModelAndView view = new ModelAndView("my view");
List<ActionHistory> histories = myService.getListData();
view.addObject("histories", histories);
return view;

In Jquery i tried couple of alternatives, first used the below line to construct JSON from List:

var list = JSON.stringify('${histories}');
console.log(histories);

the console is returning

"[com.companyname.projectname.domains.ActionHistory@48126327]"

TypeError: invalid 'in' operand a

I also tried from jquery-json by including "jquery.json.min.js" as a suggestion from this topic discussed but getting the same error above Serializing to JSON in jQuery

var histories = $.toJSON('${histories}');
console.log(histories);
Community
  • 1
  • 1
gasser
  • 279
  • 1
  • 4
  • 16
  • can u try this `var list = JSON.stringify('histories'); console.log(histories);` – Patrick Sep 18 '16 at 11:05
  • if you mean to log the list before parsing it is returning (just as coming from the controller) [com.sbm.muras.domains.ActionHistory@64b4ac3b] – gasser Sep 18 '16 at 11:08
  • set your data to a hashmap and set this hasmap reference to the json object ref and send this to the js where you can just stringify – Patrick Sep 18 '16 at 11:11
  • could you please explain what is the issue on list and why it should be working for MAP and not for list ?. – gasser Sep 18 '16 at 11:13
  • you have to print json string. javascript runs in browser long after your spring has completed it's job – charlietfl Sep 18 '16 at 12:07
  • @charlietfl that is the log of the object from java contains a list of three objects [ActionHistory [historyId=26, user=com.sbm.muras.domains.User@70419fe7], ActionHistory [historyId=25, user=com.sbm.muras.domains.User@5c9fd05e], ActionHistory [historyId=32, user=com.sbm.muras.domains.User@5c9fd05e]] – gasser Sep 19 '16 at 04:35
  • javascript and java run in different environments at different times. You have to serialize that data on server so javascript can read it in browser – charlietfl Sep 19 '16 at 04:37

1 Answers1

0

Check you contentType in ajax function it should be.

contentType: "application/json"

Also your Spring controller which is handling this mvc call should configure be configired with

produces=MediaType.APPLICATION_JSON_VALUE

e.g. something like

@RequestMapping(value ="/getList", method= RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) 
mhasan
  • 3,703
  • 1
  • 18
  • 37