用ajax发送列表到我的弹簧控制器
问题描述:
我遇到了Spring MVC和Ajax的问题。我试图给我的Spring控制器发送一个javascript列表,但我不能。我必须做一个搜索,我需要发送一个带有一些参数的列表。用ajax发送列表到我的弹簧控制器
答
你将不得不转换成列表,JSON,如果你通过AJAX发送,这From the spring blog itself:
$("#account").submit(function() {
var account = $(this).serializeObject();
$.postJSON("account", account, function(data) {
$("#assignedId").val(data.id);
showPopup();
});
return false;
});
@RequestMapping(method=RequestMethod.POST)
public @ResponseBody Map<String, ? extends Object> create(@RequestBody Account account, HttpServletResponse response) {
Set<ConstraintViolation<Account>> failures = validator.validate(account);
if (!failures.isEmpty()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return validationMessages(failures);
} else {
accounts.put(account.assignId(), account);
return Collections.singletonMap("id", account.getId());
}
}
答
你要转换您的列表,以JSON字符串,用它作为一个AJAX参数之前
答
这answer中,可能会有助于
jQuery的AJAX在客户端
$.ajax({
type: "POST",
url: "submit",
data:JSON.stringify(detailsArr),
success: function(html){
alert("Submitted");
}
});
,并在服务器端
@RequestMapping(value = "/search", method=RequestMethod.POST)
public String yourMethod(@RequestBody String body){
//convert body to array using JSONLib, FlexJSON or Gson
}
http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/ – NimChimpsky 2012-07-19 08:26:56
可能会有所帮助,如果你提供了一个简短的例子你的javascript,ajax调用,你怎么知道它不工作?你是否在服务器端或两者都得到了javascript错误或错误? – km1 2012-07-19 03:12:17