title: Calling rest webservice using jquery ajax?
body: <p>I tried couple of tutorial to call rest web-service using jQuery ajax call, but I am not getting the response.</p>
  
  <p>But When I directly hitting the url in browser I am getting the response ,but unable to get same json response using ajax call,always going in the error block.
  (tomcat is running on port 8888)</p>
  
  <p><a href="http://localhost:8888/WebService_2/rest/webservice" rel="nofollow">http://localhost:8888/WebService_2/rest/webservice</a></p>
  
  <p>Index.jsp file.</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
      pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;Insert title here&lt;/title&gt;
      &lt;/head&gt;
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt;
      &lt;body&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;input type="button" value="submit" id="submit"&gt; &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/body&gt;
  
      &lt;script type="text/javascript"&gt;
          $(document).ready(function(){
  
              $("#submit").click(function(){
                  $.ajax({
                      type: "GET",
                      dataType:"json",
                      contentType: "application/json; charset=utf-8",
                      url: "http://localhost:8888/WebService_2/rest/webservice",
                      success: function(data1) {
                          console.log("response:" + data1);
                      },
                      error: function(jqXHR, textStatus, errorThrown) {
                          console.log(' Error in processing!');
  
                      }
                  });
              });   
          });
      &lt;/script&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>WebSerivce Class.</p>
  
  <pre><code>package com.app.rest;
  
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  
  @Path("/webservice")
  public class WebSerivce {
  
      @GET
      @Produces(MediaType.APPLICATION_JSON)
      public String getResponse(){
          return "Web Service Response Call " ;
      }
  }
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;jersey-serlvet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
              &lt;param-value&gt;com.app.rest&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;jersey-serlvet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>