[英]How to pass data from Spring controller to JQuery $.post() call
After posting data from the Jquery $.post()
method, I am attempting to return some data which I want to use in the Jquery alert() from a Spring boot 1.5.1 controller method. 从Jquery
$.post()
方法发布数据后,我试图从Spring boot 1.5.1控制器方法返回一些我想在Jquery alert()中使用的数据。 Currently, the returned data is empty when used in the alert(). 目前,在alert()中使用时返回的数据为空。
Jquery: jQuery的:
$('#element').click(function() {
var formData = $('#element').serialize();
var posting = $.post( '/update.json?id=${item.id}', formData );
posting.done(function( data ) {
alert(data);
});
return false;
});
Controller: 控制器:
@RequestMapping("/update.json")
@ResponseBody
public void update(HttpServletRequest request,
Map model,
@RequestParam(value="id", required=true) Integer id) {
// to validation and binding...
model.put("result", "test");
}
Why is the result property not accessible in the Jquery data
object in the callback? 为什么在回调中的Jquery
data
对象中无法访问结果属性?
Your Controller has a return type as void, that's normal. 您的控制器的返回类型为void,这是正常的。 You can either use a response entity, if you want to set the status:
如果要设置状态,可以使用响应实体:
return new ResponseEntity<String>("test", HttpStatus.OK)
or you can return a plain string a well. 或者你可以返回一个普通的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.