gpt4 book ai didi

javascript - 如何使用 jquery ajax 检索 json 对象并每 n 秒刷新一次该对象

转载 作者:行者123 更新时间:2023-11-30 10:49:20 25 4
gpt4 key购买 nike

<分区>

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);
}

在客户端

<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>

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com