Reputation: 1245
How can I normaly pass data to MVc controller action by ajax Now in my js file
$.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");
}
});
}
}
});
and in my 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);
}
}
But I have always have Failed to load resource: the server responded with a status of 415 (Unsupported Media Type)
Upvotes: 1
Views: 6570
Reputation: 120771
Model an Object that match the ajax request, then use it as request parameter:#
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){ ... }
Upvotes: 1