且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Jackson将数组从javascript传递到java servlet

更新时间:2023-12-04 14:28:50

基本想法应该是直截了当的:

The basic idea should be straightforward:

服务器:

doPost(HttpServletRequest req, HttpServletResponse resp)
{
  ObjectMapper mapper = new ObjectMapper();
  ArrayNode rootNode = mapper.readValue(req.getReader(), ArrayNode.class);
}

客户:

使用jQuery(您也可以使用其他框架或手动)。加载 json2.js 的副本以确保您拥有JSON.stringify。

Using jQuery (you can also do it with other frameworks, or manually). Load a copy of json2.js to make sure you have JSON.stringify.

jQuery.ajax({
  type: 'POST',
  url: servletURL,
  data: JSON.stringify(jsArray),
  dataType: 'json',
  contentType: 'application/json'
});