【问题标题】:java spring boot HTTP POST request not workingjava spring boot HTTP POST请求不起作用
【发布时间】:2015-09-30 19:01:38
【问题描述】:

对于我的应用程序,我正在编写一个 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 请求。我应该如何解决这个问题?

【问题讨论】:

  • 您遇到的错误是什么?
  • 它没有显示任何错误它没有在控制台中打印输出
  • 在我的 java 控制台中.. 我的意思是在我的 Eclipse 控制台中
  • 嗨,你解决过这个问题吗?我也遇到过这个问题。

标签: java jquery ajax spring model-view-controller


【解决方案1】:

根据你的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());
}

【讨论】:

    猜你喜欢
    • 2017-08-10
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 2022-11-04
    • 2014-08-30
    • 2018-08-06
    • 2015-07-07
    • 2023-03-20
    相关资源
    最近更新 更多