I tried couple of tutorial to call rest web-service using jQuery ajax call, but I am not getting the response.
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)
http://localhost:8888/WebService_2/rest/webservice
Index.jsp file.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<body>
<table>
<tr>
<td><input type="button" value="submit" id="submit"> </td>
</tr>
</table>
</body>
<script type="text/javascript">
$(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!');
}
});
});
});
</script>
</html>
WebSerivce Class.
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 " ;
}
}
web.xml
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.app.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>