title: 415 Unsupported Media Type while invoking RESTful java web service through ajax
body: <p>I am trying an example that invokes a restful java web service taking a 'name' as input and posts 'hello name' as follows:</p>
  
  <pre><code>     @Path("/login")
       public class LoginWS {
       @POST
       @Produces(MediaType.APPLICATION_JSON)
       public NameBean getHello( NameBean nb ) throws JSONException{
           System.out.println("starting pt");
           String output="Hello "+ nb.getName();
           nb.setName(output);
           return nb;
       }
  }
  </code></pre>
  
  <p>Here is my NameBean class that is used to map JSON data:</p>
  
  <pre><code>public class NameBean {
      private String name;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  }
  </code></pre>
  
  <p>On the client side, I am using ajax to invoke this:</p>
  
  <pre><code>function Invoke(){
  
          $.ajax({
              type: "POST",
              url: 'http://localhost:8080/OnlineJudgeDemo/api/login/',
              data: JSON.stringify({
                  "name" : $("#haha").val()
              }),
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(data, textStatus, jqXHR){
                  alert("wow!");
              },
              error: function(jqXHR, textStatus, errorThrown){
                  alert("sorry! " + textStatus+errorThrown);
              }
          });
      }
  </code></pre>
  
  <p>However, when the invoke() function is called, it gives 415 Unsupported Media Type error.</p>
  
  <p>I have checked many other Stack Overflow pages regarding the same problem, but none seems to solve my query.</p>
  
  <p>Can somebody help me to know where am i going wrong?</p>