【发布时间】:2013-11-06 07:09:17
【问题描述】:
我尝试在 Spring MVC 中对我的控制器进行 AJAX 查询。
我的操作代码是:
@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "text") String text, @RequestParam(value = "userId") String userId){
//some code
}
我的 Ajax 查询是:
$.ajax({
type: "POST",
url:url,
contentType: "application/json",
data: {
start_date: scheduler.getEvent(id).start_date,
end_date: scheduler.getEvent(id).end_date,
text: scheduler.getEvent(id).text,
userId: userId
},
success:function(result){
//here some code
}
});
但我得到一个错误:
必需的字符串参数''start_date 不存在
为什么?
据我所知,我将其介绍为 (@RequestParam(value = "start_date") String start_date
UDP
现在我给 404
我的班级取数据
public class EventData {
public String end_date;
public String start_date;
public String text;
public String userId;
//Getters and setters
}
我的 js AJAX 调用是:
$.ajax({
type: "POST",
url:url,
contentType: "application/json",
// data: eventData,
processData: false,
data: JSON.stringify({
"start_date": scheduler.getEventStartDate(id),
"end_date": scheduler.getEventEndDate(id),
"text": scheduler.getEventText(id),
"userId": "1"
}),
和控制器动作:
@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestBody EventData eventData){
}
而 JSON 数据是:
end_date: "2013-10-03T20:05:00.000Z"
start_date: "2013-10-03T20:00:00.000Z"
text: "gfsgsdgs"
userId: "1"
【问题讨论】:
-
jquery 是否将
data元素序列化为请求参数?作为 url-encoded-form 参数? -
只是JS文件中的变量
-
在您的两个示例中,您是否尝试过实际检查您的 POST 请求并确保它传递了所需的值?在每种情况下都显示了什么?
标签: java ajax spring spring-mvc