Javascript 如何在jQueryAjax调用中使用数组字符串?
通过使用jQuery,我试图在后端保存我的字符串数组,下面是我的方法 1.使用jQuery ajax发送数组Javascript 如何在jQueryAjax调用中使用数组字符串?,javascript,java,jquery,arrays,spring,Javascript,Java,Jquery,Arrays,Spring,通过使用jQuery,我试图在后端保存我的字符串数组,下面是我的方法 1.使用jQuery ajax发送数组 var tagTexts = $(ul li.select2-selection__choice") .toArray() .map(function(i){ return i.innerText; }); tagTexts =
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;
}
Drone,Richard Feynman, PHD,Yatch
[Drone, Richard Feynman, PHD, Yatch]
["Drone", "Richard feynman, PHD", "Yatch"]
[["Drone", "Richard feynman, PHD", "Yatch"]].
在使用数组联接方法后,将对其进行如下更改:
@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;
}
Drone,Richard Feynman, PHD,Yatch
[Drone, Richard Feynman, PHD, Yatch]
["Drone", "Richard feynman, PHD", "Yatch"]
[["Drone", "Richard feynman, PHD", "Yatch"]].
在java执行(后端)中,这将显示如下:
@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;
}
Drone,Richard Feynman, PHD,Yatch
[Drone, Richard Feynman, PHD, Yatch]
["Drone", "Richard feynman, PHD", "Yatch"]
[["Drone", "Richard feynman, PHD", "Yatch"]].
b)使用JSON.stringify方法
使用JSON.stringify方法后,捕获的数组显示如下:
@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;
}
Drone,Richard Feynman, PHD,Yatch
[Drone, Richard Feynman, PHD, Yatch]
["Drone", "Richard feynman, PHD", "Yatch"]
[["Drone", "Richard feynman, PHD", "Yatch"]].
这在js级别看起来很好,但整个行被视为一个字符串,它在后端显示如下:
@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;
}
Drone,Richard Feynman, PHD,Yatch
[Drone, Richard Feynman, PHD, Yatch]
["Drone", "Richard feynman, PHD", "Yatch"]
[["Drone", "Richard feynman, PHD", "Yatch"]].
我希望我的代码按如下方式工作:
@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;
}
Drone,Richard Feynman, PHD,Yatch
[Drone, Richard Feynman, PHD, Yatch]
["Drone", "Richard feynman, PHD", "Yatch"]
[["Drone", "Richard feynman, PHD", "Yatch"]].
有没有更好的方法来实现这一点 使用JSON.stringify的javascript是正确的解决方案。您只需要在java后端解析JSON。比如说,
import org.json.simple.JSONObject;
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(tagTexts);
有关如何在java中解析JSON的其他方法,请参阅。您可以创建如下自定义请求对象:
public class TagListRequest {
private List<String> tagTexts;
// getter and setter
}
Spring使用将请求的JSON主体内部映射到TagListRequest
对象
使用此解决方案,您不需要使用分隔符手动解析数据