Restful Service (from Server)
@GET
@Produces("application/json")
@Consumes("application/json")
@Path("/getStatus/")
// server:8080/server/rest/admin/status/whatSoEver
public void getStatus(
@Context HttpServletRequest request,
@Context HttpServletResponse response) throws ServletException,
IOException
{
//create the JSON Object to pass to the client
JSONObject object=new JSONObject();
response.setContentType("text/javascript");
String callback = request.getParameter("jsoncallback");
try
{
for (Server i : svr)
{
object.put("name",getName());
object.put("status",getStatus());
}
}
catch(Exception e)
{
throw new ServletException("JSON Hosed up");
}
String json = object.toString();
/*response.getOutputStream().println(callback + "(" + json + ");");*/
response.getOutputStream().print(json);
response.flushBuffer();
System.out.println("Sending "+json);
}
On the client side
<script>
$(document).ready(function()
{
function myFunc()
{
$.getJSON("http://localhost:8080/MyWebApp/getStatus",
function (json)
{
$('#refreshMe').replaceWith(json.status);
/* alert("Server name: " + json.name + "Server Status:"+json.status); */
});
}
myFunc();
});
/*Do a refresh every 2 seconds */
setInterval( "myFunc()", 500 );
</script>
How do I get this jquery ajax call to
behave the same as the above?
<script>
$.ajaxSetup ({ cache: false });
$.ajax
({
url: "http://localhost:8080/MyWebApp/getStatus",
dataType: 'json',
data: "???",
success: ???
});
</script>
Like so: