2013-10-27 19 views
13

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" 
+0

jquery serializza gli elementi 'data' come parametri di richiesta? Come parametri della forma con codifica url? –

+0

È solo variabile dal file JS – nabiullinas

+0

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

risposta

28

Sul lato server che ci si aspetta i parametri di richiesta come stringhe di query, ma sul lato client si invia un oggetto JSON. Per associare un json devi creare una singola classe contenente tutti i tuoi parametri e utilizzare l'annotazione @RequestBody anziché @RequestParam.

@RequestMapping(value = "events/add", method = RequestMethod.POST) 
public void addEvent(@RequestBody CommandBean commandBean){ 
    //some code 
} 

Here is una spiegazione più dettagliata.

+0

L'ho provato. Dà il mio errore 400 Richiesta errata – nabiullinas

+0

Puoi pubblicare il JSON effettivo che stai tentando di inviare? Puoi catturarlo con Firebug o Chrome/Chromium. – gadget

+1

Puoi scrivere, come posso vederlo in Google Chrome? – nabiullinas

0

Ho avuto lo stesso problema .. Ho risolto specificando i params config nella richiesta POST:

var config = { 
    transformRequest : angular.identity, 
    headers: { "Content-Type": undefined } 
} 

$http.post('/getAllData', inputData, *config*).success(function(data,status) { 
    $scope.loader.loading = false; 
}) 

config è stato il parametro ho incluso ed è iniziato a lavorare .. Speranza che aiuta:

Problemi correlati