2015-07-12 128 views
1

對於我的應用程序,我正在編寫POST請求以從複選框列表中發送參數數組。它的工作獲取請求,但不適用於發佈請求。我的代碼中有什麼錯誤。java spring boot HTTP 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請求。如何解決此問題?

+0

什麼是你所得到的錯誤? – Karthik

+0

它不顯示任何錯誤,它不打印輸出在服務器端的控制檯 –

+0

? – Karthik

回答

0

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