gpt4 book ai didi

java - 通过ajax将数据传递给spring mvc Controller

转载 作者:太空宇宙 更新时间:2023-11-04 07:18:43 25 4
gpt4 key购买 nike

我怎样才能正常通过ajax将数据传递给MVc Controller Action 现在在我的 js 文件中

$.ajax({
type: "POST",
url:url,
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){
if(combo.getActualValue() != null){
getUserEvents(id);
}
else{
$.ajax({
url:"/WebElanceSh/events",
success:function(result){
json = result;
scheduler.parse(json, "json");
}
});
}
}
});

在我的 Controller 中

@RequestMapping(value = "events/add/", method = RequestMethod.POST)
public void addEvent(@RequestBody String start_date,
@RequestBody String end_date,
@RequestBody String text,
@RequestBody Integer userId){
Event event = new Event(text,start_date,end_date);

if(userId == -1){
TestData.getInstance().AddEvent(-1, event);

}
else {
TestData.getInstance().AddEvent(userId, event);

}

}

但我一直都有无法加载资源:服务器响应状态为 415(不支持的媒体类型)

最佳答案

建模一个与ajax请求匹配的对象,然后将其用作请求参数:#

public class MyCommandObject {
private Date start_date;
private Date end_date;
private String text;
private Integer userId;

/** Constructor wihtout parameter needed. */
public MyCommandObject() {}

Getter and Setter
}


@RequestMapping(value = "events/add/", method = RequestMethod.POST)
public void addEvent(@RequestBody MyCommandObject command){ ... }

关于java - 通过ajax将数据传递给spring mvc Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19618208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com