2013-02-13 3 views
5

Ciao Sto cercando di inviare dati json a WS riposante implementato con Jersey. Sto inviando i dati tramite jquery-ajax. Perché sto ricevendo il tipo di supporto HTTP Status-415 non supportato? Grazie.
Click here for screenshot of firebug descriptionStato HTTP 415 - Tipo di supporto non supportato per chiamate AJAX in JQuery a WS restful implementato con JERSEY

//post method handler 
     @Path("/newentry") 
     public class NewEntry { 

      @POST 
      @Consumes(MediaType.APPLICATION_JSON) 
      public Response newEntry(String data) { 
        //doStuff 
     } 
    } 
    // ajax call 
     $.ajax({ 
       url: "http://localhost:8080/FirstRestWebService/rest/newentry", 
       type: "post", 
       data: formToJSON(), 
       dataType : "json", 
       success: function(data){ 
       alert("success"); 
        }, 
       error:function(jqXHR, textStatus, errorThrown) { 
        alert("failure"); 
        } 
      }); 

     function formToJSON() { 
       return JSON.stringify({ 
        "name": $("input#emp_name").val(), 
        ... 
        "username": $('input#username').val(), 
        "password": $('input#password').val() 
        }); 

Click here for screenshot of firebug description sono stato in grado di testare il WS con successo da Jersey Cliente. Cosa c'è di sbagliato nella suddetta chiamata AJAX? Grazie.

risposta

17

Nella vostra chiamata AJAX è necessario impostare il tipo di contenuto:

contentType: "application/json" 
+0

Grazie mille. – mik

1

è necessario dichiarare la dipendenza JSON. Prova ad aggiungere la seguente dipendenza al tuo pom.xml.

<dependency> 
    <groupId>com.owlike</groupId> 
    <artifactId>genson</artifactId> 
    <version>0.98</version> 
</dependency>