我需要在一个页面中添加ajax功能。但它不适用于 spring mvc 中的 @PathVariable。
page1.jsp
<li><a href="page2/sss">WatchEvent</a></li>
1)工作正常 在 Controller 中
@RequestMapping(value = "page2/{id}", method = RequestMethod.GET)
public ModelAndView WatchEvent(@PathVariable("id")String id) {
System.out.println("In getplayback :"+id);
List<modelone> getdetails=Serviceone.detailsToUser(id);
System.out.println("In getplayback from db:"+getdetails);
return new ModelAndView("page2","getdetails",getdetails);
}
此处详细信息发送至第2页。
2))但是没有说k。
Page2.jsp
<script type="text/javascript">
$(document).ready(function() {
alert("ready");
var fun="event";
var savedata = {
action:fun,
};
$.ajax({
url : "events",
type : "POST",
datatype:"json",
data: savedata,
error:function(){
alert("Error");
},
success: function() {
alert("success");
}
});
});
</script> `
但不会转到下面的 Controller 。
@RequestMapping(value="events",method=RequestMethod.POST)
public @ResponseBody String GetSomeEvents(){
System.out.println("In someevent********************");
String todayDate = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
System.out.print("today"+todayDate);
List<modelone> getlist=Serviceone.getSomeEvents();
return "success";
}
结果是警报错误。
在 Firebug 中 发布....../page2/事件
405 方法不允许
最佳答案
按如下方式修改您的操作后尝试:
@RequestMapping(value = "events", method = RequestMethod.POST)
public @ResponseBody String GetSomeEvents(@RequestParam String action) {
System.out.println("In someevent********************");
String todayDate = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
System.out.print("today" + todayDate);
List<modelone> getlist = Serviceone.getSomeEvents();
return "success";
}
修改是将您的请求参数添加到您的操作参数中。
关于jquery - spring mvc中使用@PathVariable时ajax不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27184330/