【发布时间】:2015-03-02 19:22:30
【问题描述】:
/*ajax request to servlet to perform update operation*/
var savedata={
video_Title:videotitle,
video_duration:videoduration,
video_Url:videourl,
video_Description:videodescription
};
$.ajax({
url:'videoUpdate',
type:'POST',
cache:false,
data: savedata,
contentType: "application/json; charset=utf-8",
success: function(response) {
alert("Updated Successfully");
},
error:function()
{
alert("oops sorry something went wrong. we apologize for the inconvenience");
}
});
/*Controller Class*/
@RequestMapping(value ="videoUpdate",method = RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Status updateVideo(@RequestBody Video video) {
try {
System.out.println("update servlet");
dataServices.updateVideo(video);
return new Status(1,"video updated Successfully");
}
catch (Exception e) {
return new Status(0,e.getMessage().toString());
}
}
我的问题是如何在我的servlet类中接收ajax-json请求数据。这是在spring mvc中从ajax捕获一组数据的正确方法吗。或者需要其他注释来避免400错误!?
【问题讨论】:
-
记录您的错误,您将获得有关问题所在的实际信息。
-
@codeseeker 当您使用
@RequestBody Video video时,您发送的数据是否与模型类绑定?似乎更多的请求数据不匹配?