Provo a creare una query AJAX sul controller in Spring MVC.Il parametro String richiesto non è presente in Spring MVC
mio codice di azione è:
@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
}
La mia domanda è 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
}
});
ma ho ottenuto un errore:
Required String parameter ''start_date is not present
Perché? Come so ho presentato come (@RequestParam(value = "start_date") String start_date
UDP
Ora mi danno 404 La mia classe di prendere i dati
public class EventData {
public String end_date;
public String start_date;
public String text;
public String userId;
//Getters and setters
}
Il mio invito 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"
}),
e regolatore azione:
@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestBody EventData eventData){
}
e JSON dei dati è:
end_date: "2013-10-03T20:05:00.000Z"
start_date: "2013-10-03T20:00:00.000Z"
text: "gfsgsdgs"
userId: "1"
jquery serializza gli elementi 'data' come parametri di richiesta? Come parametri della forma con codifica url? –
È solo variabile dal file JS – nabiullinas
In entrambi i casi, hai provato a verificare effettivamente la tua richiesta POST e assicurandoti che stia passando i valori desiderati? Cosa mostra in ogni caso? – woemler