Retrieve data from handsontable to Struts2 Action via JSON not working

匆匆过客 提交于 2019-12-03 00:21:49

I've fixed it.

1: In struts.xml add:

<interceptor-ref name="json">
    <param name="enableSMD">true</param>
</interceptor-ref>

2: In Ajax request add:

contentType: 'application/json'

3: Change JSON format which is automatically bad formatted by handontable. There were in JSON some characters like: %5B %5D %7B %7D %22 instead of: [ ] { } "

I've replaced them by own fixedEncodeURI() function:

var data = '{"data":' + fixedEncodeURI(JSON.stringify(handsontable.getData())) + '}';
$.ajax({
    url: "../json/saveJSONDataAction.action",
    data: data, //returns all cells' data
    dataType: 'json',
    contentType: 'application/json',
    type: 'POST',
    success: function (res) {
    }
  });

function fixedEncodeURI (str) {
    return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%7B/g, '{').replace(/%7D/g, '}').replace(/%22/g, '"');
}

I've not looked at your code thoroughly, but at first look it seems that you are erasing the data collection when you create a new one with that new ArrayList<Report>() in your Action. Just leave alone the declaration private List<Report> data;.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!