2015-03-21 22 views
5

sto affrontando l'errore:AJAX POST a Spring MVC controller non funziona

Failed to load resource: the server responded with a status of 415 (Unsupported Media Type)

AJAX parte del mio codice è il seguente:

$.ajax({ 
    url: '/authentication/editUser',  
    type: "POST", 
    contentType: "application/json", 
    data: JSON.stringify(requestObj), //Stringified JSON Object 

    success: function(resposeJsonObject) { 
     // 
    } 
}); 

E metodo del gestore del controller:

@RequestMapping(value = "/editUser", method = RequestMethod.POST, 
    headers = {"Content-type=application/json"}) 
@ResponseBody 
public EditUserResponse editUserpost(@RequestBody EditUserRequest editUserRequest) { 
    System.out.println(editUserRequest); 
    return new EditUserResponse(); 
} 

Come risolvere l'errore?

+0

Provare a passare il tipo di contenuto nelle intestazioni. –

+0

viene eseguito nella chiamata ajax come contentType: "application/json" –

+0

Sto dicendo di passarlo separatamente nelle intestazioni come 'intestazioni: { 'Accept': 'application/json', 'Content-Type': ' application/json ' }, qualcosa come questo. –

risposta

0

Cambia la tua chiamata AJAX, aggiungere intestazioni:

headers: { 
    'Accept': 'application/json', 
    'Content-Type': 'application/json' 
} 

La chiamata generale AJAX dovrebbe apparire così:

$.ajax({ 
    url: '/authentication/editUser',  
    type: "POST", 
    headers: { 
     'Accept': 'application/json', 
     'Content-Type': 'application/json' 
    } 
    dataType: "json", 
    data: JSON.stringify(requestObj), //Stringified JSON Object 

    success: function(resposeJsonObject) {    
     // 
    } 
}); 

L'intestazione Content-Type viene utilizzato da @RequestBody per determinare quale formato i dati inviati . set

+0

stesso errore persiste - POST http: // localhost: 8080/authentication/editUser 415 (Tipo supporto non supportato) –

+0

Hai aggiunto 'dataType:" json ",' ? –

+0

sì, l'ho fatto. –

1

manualmente Content-Type e Accept in beforeSend gestore della funzione AJAX, e rimuovere le intestazioni di vostra primavera metodo del gestore di controllo per farlo funzionare come: funzione

AJAX:

$.ajax({ 
    url: '/authentication/editUser', 
    type: 'POST', 
    data: JSON.stringify(requestObj), //Stringified Json Object 
    beforeSend: function(xhr) { 
     xhr.setRequestHeader("Accept", "application/json"); 
     xhr.setRequestHeader("Content-Type", "application/json"); 
    } 
    success: function(resposeJsonObject){} 
}); 

e regolatore metodo del gestore come:

@RequestMapping(value = "/editUser" , method = RequestMethod.POST) 
public @ResponseBody EditUserResponse editUserpost(@RequestBody EditUserRequest editUserRequest){ 
    System.out.println(editUserRequest); 
    return new EditUserResponse(); 
} 

Vedere anche:

0

penso che si dovrebbe cercare di rimuovere intestazione controller come questo:

@RequestMapping(value = "/editUser" , method = RequestMethod.POST) 
0

Prova questa .. ho cambiato nel controller e jQuery .. spero funzioni

$.ajax({ 
     url: '/authentication/editUser',  
     type: "POST", 
     contentType: "application/json", 
     data: $("#FormName").serialize() 

     success: function(respose) { 
      // 
     } 
    }); 

A e il metodo di gestione del controllore:

@RequestMapping(value = "/editUser", method = RequestMethod.POST, 
     headers = {"Content-type=application/json"}) 
// removed @RequestBody 
    @ResponseBody 
    public EditUserResponse editUserpost(EditUserRequest editUserRequest) { 
     System.out.println(editUserRequest); 
     return new EditUserResponse(); 
    }