【问题标题】:How do I use jquery ajax to retrieve a json object and refresh this object every n seconds [closed]如何使用 jquery ajax 检索 json 对象并每 n 秒刷新一次该对象 [关闭]
【发布时间】:2011-06-17 13:07:16
【问题描述】:

Restful 服务(来自服务器)

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

如何让这个 jquery ajax 调用 行为和上面一样吗?

<script>


   $.ajaxSetup ({ cache: false  });  

    $.ajax
    ({
        url: "http://localhost:8080/MyWebApp/getStatus",
        dataType: 'json',
        data: "???",
        success: ???
    });

</script>

【问题讨论】:

  • 请不要将 setInterval 与字符串一起使用,而是使用函数指针:setInterval(myFunc, 500);setInterval(function(){ /*some code*/ }, 500); 此外,您可能希望在前一个请求完成而不是更早时开始新请求。
  • 嗨,这是我想做的一个很好的例子。您使用哪些 Java 库来构建 REST 服务?

标签: javascript jquery ajax jsp


【解决方案1】:

像这样:

    <script>
    $.ajaxSetup({
        cache: false
    });

    $.ajax({
        url: "http://localhost:8080/MyWebApp/getStatus",
        dataType: 'json',
        success: function(json) {
            $('#refreshMe').replaceWith(json.status);
        }
    });
    </script>

【讨论】:

  • 谢谢!这就像一个魅力
  • 不客气。不要忘记接受答案:-)
  • 最后一个问题。为了使这个 ajax 请求不断启动,我是在 $.ajax 语句中设置Interval 还是在 $.ajax 调用周围嵌套另一个函数并在该调用上嵌套另一个函数
  • 我会在 success 回调中执行此操作,只是因为它可能需要 2 秒以上的时间才能回复,然后你会在那里遇到竞争问题。
【解决方案2】:

只需为成功属性分配一个将被调用的函数

$.ajax
({
    url: "http://localhost:8080/MyWebApp/getStatus",
    dataType: 'json',
    success: function(response, status, XHR) {
        $('#refreshMe').replaceWith(status);
    }
});

【讨论】:

  • 谢谢 Rajaa,我现在可以看到请求通过了。那么如何从这里获取 json 对象的属性呢? json.name?
  • 这些都是来自服务器的对象的一部分,将存储在响应变量中。您可以通过response.name .... 和response.status ... 的响应对象访问它
猜你喜欢
  • 2014-10-16
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多