对于我的应用程序,我编写了一个POST请求来发送复选框列表中的参数数组。它适用于get请求,但不适用于post请求。我的代码中的错误是什么。
我在客户端用于向服务器发送ajax请求的代码。
$(".add").click(function(){
monitoring.length=0;
nonMonitoring.length=0;
$('.modal-body input:checked').each(function() {
monitoring.push($(this).val());
});
$('.addkeywords input:checked').each(function() {
nonMonitoring.push($(this).val());
});
// alert(monitoring[2]+ " " + nonMonitoring[2]);
var monitoringLength=monitoring.length;
var nonMonitoringLength=nonMonitoring.length;
$.ajax({
type : "POST",
url : '/rest/channelstats/my/rest/controller',
data : {
// monitoring : monitoring,
// nonMonitoring: nonMonitoring,
monitoringLength: monitoringLength,
nonMonitoringLength: nonMonitoringLength,
},
success : function(data) {
// var keywordsList=data
//console.log(keywordsList);
// htm = "" ;
}
});
})我在服务器端的java代码。
@RequestMapping(value="/rest/channelstats/my/rest/controller",method = RequestMethod.POST)
public void monitorKeywords(@RequestParam(value="monitoringLength",required=true)int monitoringLength,@RequestParam(value="nonMonitoringLength",required=true)int nonMonitoringLength){
System.out.println("MonitoringLength =>" +monitoringLength);
System.out.println("NonMonitoringLength=>" +nonMonitoringLength);
}
}它适用于HTTP GET请求,但不适用于POST requests.How,我应该解决这个问题吗?
发布于 2015-11-02 02:19:35
根据您的jquery post请求,您应该使用DAO(Data Access Object)来解析请求数据。因此您应该添加类Request
public class Request {
private int monitoringLength;
private int nonMonitoringLength;
//getters and setters
} 并将控制器更改为
@RequestMapping(value="/rest/channelstats/my/rest/controller",method = RequestMethod.POST)
public void monitorKeywords(@RequestBody Request request){
System.out.println("MonitoringLength =>"+request.getMonitoringLength());
System.out.println("NonMonitoringLength=>"+request.getNonMonitoringLength());
}https://stackoverflow.com/questions/31367704
复制相似问题