通过使用jQuery,我试图在后端保存我的字符串数组,以下是我的方法。
1.使用jQuery ajax发送数组。
var tagTexts = $(ul li.select2-selection__choice") .toArray() .map(function(i){ return i.innerText; });tagTexts = tagTexts.join(',');$.ajax({ type: 'POST' , url: '/tag/tagList', dataType: 'json', data: { tagTexts: tagTexts }, error: function(err) { console.log(err); }, success: function(response) { //Process Response }});
2.在后端,检索如下:
@ResponseBody@RequestMapping(value = "/tag/tagList", method = RequestMethod.POST)public String saveTagList(HttpServletRequest request, @RequestParam(value = "tagTexts", required = false)List<String>tagTexts) { System.out.println(tagTexts); String response = tagService.saveTags(tagTexts); return response;}
a)使用数组连接方法
以下是捕获的字符串数组:
["Drone", "Richard Feynman, PHD", "Yatch"]
使用数组连接方法后,它会更改如下:
Drone,Richard Feynman, PHD,Yatch
在java执行(后端)中,它显示如下:
[Drone, Richard Feynman, PHD, Yatch]
b)使用JSON.stringify方法
使用JSON.stringify方法后,捕获的数组显示如下:
["Drone", "Richard feynman, PHD", "Yatch"]
这在js级别看起来很好但是这整行被认为是一个字符串,它在后端显示如下:
[["Drone", "Richard feynman, PHD", "Yatch"]].
我期待我的代码工作如下:
捕获的原始数据元素中的逗号字符不应拆分。
整个数组不应该在后端显示为字符串。
有没有更好的方法来实现这一目标?
开满天机
相关分类