我试图通过Spring MVC对Java方法进行ajax调用(返回一个String)。下面这个javascript代码成功调用Java方法并打印出我对eclipse控制台的期望。我正在尝试输出我刚才在html页面上提到的文本。我的问题是成功或错误回调函数都没有输入,所以很明显我无法获得Java方法返回的信息。
提前致谢
$("#dateButton").click(function(e){
fromdate = $("input#fromdate").val();
todate = $("input#todate").val();
fromcurr = $("input#fromcurr").val();
tocurr = $("input#tocurr").val();
var dataString = "fromdate=" +fromdate+"&todate="+todate+"&fromcurr="+fromcurr+"&tocurr="+tocurr;
$.ajax({
type: "post",
url: "getDailyRates.html",
data: dataString,
datatype: "text",
async: 'false',
success: function(data, textStatus, jqXHR){
alert("in success");
$('#display').html("");
$('#display').append(data);
},
error: function(){
alert("ERROR!");
}
});
});
Java方法
@RequestMapping(value = "/getDailyRates.html", method = RequestMethod.POST)
public String getDailyRates(HttpServletRequest request, HttpServletResponse response ) {
DailyRatesQuery dailyRate= new DailyRatesQuery();
String fromCurr= request.getParameter("fromcurr");
String toCurr= request.getParameter("tocurr");
String fromDate= request.getParameter("fromdate");
String toDate=request.getParameter("todate");
dailyRate.setFromCurr(fromCurr);
dailyRate.setToCurr(toCurr);
dailyRate.setFromDate(fromDate);
dailyRate.setToDate(toDate);
System.out.println("From Currency: "+fromCurr+" To Currency: "+toCurr+
" fromDate: "+fromDate+" toDate: "+toDate);
List<DailyRate> rates = jdbcRateDao.getDailyRates(dailyRate);
System.out.println("Before return new model");
System.out.println(rates.toString());
return rates.get(0).toString();
}
更新:
当我单击JavaScript函数映射到的按钮时,我发布的Java方法被成功调用并生成正确的输出。它只是没有将输出发送回我的JSP。