![](/img/trans.png)
[英]How to return list of objects from controller and display them using JQuery (Spring MVC, ajax)
[英]Passing a list of strings in a Spring MVC controller using Jquery Ajax
我正在嘗試使用 JQuery Ajax 將字符串列表傳遞給 MVC controller,但我收到錯誤No mapping for POST /myUrl/myContext/p
這是我的 jquery function:
$('#myButton').on('click', function(){
var strings = [];
$('#myForm input[type=checkbox]:checked').each(function(){
var string = $(this).closest('tr').find('#mySpan').text();
strings.push(string);
});
$.ajax({
url : 'myContext/p',
dataType : 'json',
type: 'POST',
data : {strings : strings},
success: function(response) {
//my success function
}
},
error: function(e) {
//my error function
}
}
});
})
這是我的 controller:
@PostMapping(value="/myContext/p")
public ResponseEntity<MyResponse> doPost(
@RequestParam(value="strings" ,required=true) List<String> strings)
throws Exception{
MyResponse response = new MyResponse();
//my Code
response.setData(strings);
return new ResponseEntity<MyResponse>(response, HttpStatus.OK);
}
我通常會使用 @RequestBody 而不是 @RequestParam 作為字符串參數。
我解決了這個問題:
@PostMapping(value="/myContext/p", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<MyResponse> doPost( ArrayList<String> strings) throws Exception{
MyResponse response = new MyResponse();
//code
response.setData(strings);
return new ResponseEntity<RestResponse>(response, HttpStatus.OK);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.