title: jQuery Ajax POST call with JSON in Rest Web Service Issue
body: <p>I want to post a JSON object from my page to a Rest WS. But when I am posting json through jQuery ajax call as output I am getting a HTML page with "<strong>HTTP Status 405 - Method Not Allowed</strong>" instate of JSON, which I am sending from Rest Web Service. Please refer the following code snippet.</p>
  
  <p>I am using jquery 1.11.3 version.</p>
  
  <p><a href="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" rel="nofollow noreferrer">http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js</a></p>
  
  <p>jQuery Ajax Call:</p>
  
  <pre><code> $.ajax({
          url: "http://localhost:8080/MyWebService/api/myService/jsonpost",
          method: "POST",
          data: jsonObj,
          dataType: 'application/json',
          contentType: "application/json",
           success: function(result){
                alert(result);
           },
           error(){
               console.log('Error');
           }
      });
  </code></pre>
  
  <p>Please note my rest web service is running in my local tomcat.</p>
  
  <p>Please find my Rest Web Service POST code.</p>
  
  <pre><code>@POST
  @Path("/jsonpost")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public String crunchifyJsonPostREST(SampleVO input) throws JSONException{
      SampleVO sampleVO = input;
      return new Gson().toJson(sampleVO);
  }
  </code></pre>
  
  <p>VO:</p>
  
  <pre><code>public class SampleVO {
  
  private String name;
  
  /**
   * @return the name
   */
  
  @XmlElement
  public String getName() {
      return name;
  }
  
  /**
   * @param name the name to set
   */
  public void setName(String name) {
      this.name = name;
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>My Maven Dependency is:</strong></p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
          &lt;version&gt;1.8&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-json&lt;/artifactId&gt;
          &lt;version&gt;1.8&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p><strong>Firebug Details:</strong></p>
  
  <p>Please find my ajax request Header.</p>
  
  <p><img src="https://i.stack.imgur.com/UkdMG.jpg" alt="enter image description here"></p>
  
  <p>Please find my ajax response and response HTML.
   <img src="https://i.stack.imgur.com/b8mzH.jpg" alt="enter image description here">
  <img src="https://i.stack.imgur.com/16pO3.jpg" alt="enter image description here"></p>
  
  <p>Need your help to resolve this issue.</p>
  
  <p>Thanks is advance.</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>After lot of googling I have found some solution. Now I am using HTTP-Proxy-Servlet. </p>
  
  <p>I have crated a java web application with my html page which has the ajax call and from my ajax call I am calling a URL of same domain. Please refer my ajax call.</p>
  
  <pre><code> $.ajax({
          url: "rest/api/crunchifyService/jsonpost",
          method: "POST",
          data: JSON.stringify(jsonObj),
          dataType: 'json',
          contentType: "application/json",
           success: function(result,status,jqXHR ){
                //Do something
           },
           error(jqXHR, textStatus, errorThrown){
               //Do something
           }
      }); 
  </code></pre>
  
  <p>Now I have done this same domain call mapping with org.mitre.dsmiley.httpproxy.ProxyServlet. Please refer my web xml.</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;proxy&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.mitre.dsmiley.httpproxy.ProxyServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;targetUri&lt;/param-name&gt;
          &lt;param-value&gt;&lt;!-- Cross domain URL goes here --&gt;&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;log&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;proxy&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>Now my ajax is calling this Proxy Servlet and it is redirecting to the CROS destination Rest Web Service.</p>
  
  <p>Please refer the following URL, you will get more details.</p>
  
  <p><a href="https://github.com/mitre/HTTP-Proxy-Servlet" rel="nofollow noreferrer">https://github.com/mitre/HTTP-Proxy-Servlet</a></p>