我需要访问java中通过ajax传递的javascript数组对象。我正在使用道场。
这就是我尝试通过 dojo ajax 调用传递值的方法。
Javascript
var arrayObj = [{'id': 4, 'label': 'first', 'value': 'success'}, {'id': 6, 'label': 'second', 'value': 'failed'}];
dojo.xhrPost({
url: "test/deleteItems.json",
content: { items: arrayObj },
handleAs: "json",
load: function(response) {
alert('got response');
}
});
在Java(Spring MVC)中,我尝试使用java Controller
中的字符串列表
、字符串数组
来访问 items
通过 Javascript Ajax 传递。但他们都没有能够检索从 Javascript 传递的精确对象。
Java
@RequestMapping(value="deleteItems", produces = "application/json")
public @ResponseBody String deleteItems(
@RequestParam(required = true) String[] items
//@Valid @RequestBody List<AnalysisBean> pcptBean
) {
/* I need to access the array objects passed from javascript here. */
}
如何在java Controller 中检索数组对象?
注意:我尝试通过 Ajax 将多维数组传递到 java Controller 。
var arrayObj = [[4,'first', 'success'],[6, 'second', 'failed']];
但它也不起作用。
我想使用 Dojo/jQuery 获得解决方案。任何帮助将不胜感激。
最佳答案
您可以使用Jackson库中的ObjectMapper
类来解析对象数组。
您可以采用以下方法:
ObjectMapper mapper = new ObjectMapper();
YourClass[] objects = mapper.readValue(items, YourClass.class);
BR。
关于java - 我们如何在java中通过ajax访问javascript的数组对象呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21062014/