我需要使用jquery从视图向spring mvc控制器发送数据。 Spring mvc将在db中插入/更新这些值。我使用下面的代码
JQuery的:
var json = {"KPI" : visual, "partnerName": params['partnerName'],
"partnerAddress" : params['partnerAddress'], "documentType" : params['documentType'],
"itemQualifier": getItemQualifier(),"itemIdentifiers" : params['itemIdentifiers'],
"currency" : getCurrency(), "sellSide": getSellSide()};
$.ajax({
type: "POST",
url: "/reports/savefilters",
data: json,
success: function(response){
alert('success: ' + response);
},
error: function(e){
alert('Error: ' + e);
}
});
Spring MVC控制器代码:
@RequestMapping(value="/savefilters", method=RequestMethod.POST)
public String saveFilters(@RequestBody AnalyticsFilters filters){
System.out.println("savefilters method in controller");
System.out.println("KPI:"+filters.getKPI());
if(filters.getPartnerName() != null && filters.getPartnerName().size()>0 ) System.out.println("partnerName:"+filters.getPartnerName().get(0));
if(filters.getPartnerAddress() != null && filters.getPartnerAddress().size()>0 ) System.out.println("partnerAddress:"+filters.getPartnerAddress().get(0));
if(filters.getDocumentType() != null && filters.getDocumentType().size()>0 ) System.out.println("documentType:"+filters.getDocumentType().get(0));
System.out.println("itemQualifier:"+filters.getItemQualifier());
if(filters.getItemIdentifiers() != null && filters.getItemIdentifiers().size()>0 ) System.out.println("itemIdentifiers:"+filters.getItemIdentifiers().get(0));
System.out.println("currency:"+filters.getCurrency());
System.out.println("sellSide:"+filters.getSellSide());
new DashboardDisplayService().saveFilters();
return "successful";
}
发布请求将转换为Get方法,无法访问spring mvc控制器。我尝试了不同的ajax json选项,如datatype,contenttype等。如何确保POST请求不会更改为GET方法???
答案 0 :(得分:0)
Ajax方法返回ResponseBody中的消息。我修改了你的处理程序方法,如下所示。
@RequestMapping(value="/savefilters", method=RequestMethod.POST)
public @ResponseBody String saveFilters(@RequestBody AnalyticsFilters filters){
...
}