JQuery从servlet获取JSON

use*_*458 2 html jquery json servlets

我想创建一个从服务器(servlet)获取数据的进度条.所以我创建了一个html文件和一个创建json数据的servlet(静态用于测试目的)

  <script>
  $(document).ready(function() {
    $.getJSON("GetProgressEvent", function(data) {     
    $.each(data.ProgressEvents, function(){
    $("#progressbar").progressbar({ value: this.progress });
     $("#progressStatus").html(this.status);
    });
    // setTimeout(arguments.callee, 500);
    });
  });
  </script>
</head>
<body style="font-size:62.5%;">

<div id="progressbar"></div>
<div id ="progressStatus"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Servlet GetProgressEvent.java:

 @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
   {
      resp.setContentType("application/json");
      resp.setCharacterEncoding("utf-8");
      PrintWriter out = resp.getWriter();
      out.println("{'ProgressEvents':[{'progress':"
         + 10
         + ",'status':'10%'},{'progress':"
         + 20
         + ",'status':'20%'},{'progress':"
         + 30
         + ",'status':'30%'}}]}");
      out.close();

   }
Run Code Online (Sandbox Code Playgroud)

用firebug我注意到servlet正常发送数据,发送的数据如下:

{'ProgressEvents':[{'progress':10,'status':'10%'},{'progress':20,'status':'20%'},{'progress':30,'status':'30%'}}]}
Run Code Online (Sandbox Code Playgroud)

但循环永远不会执行的问题,我不明白为什么.我刚开始使用jQuery,我需要你的帮助,任何帮助都是预先感谢你的帮助:)

Jam*_*urz 5

问题是'无效json你需要双引号.

您可以使用http://jsonlint.com/验证json