JQuery从servlet获取JSON [英] JQuery Get JSON from servlet

查看:77
本文介绍了JQuery从servlet获取JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



$ b

 < script> 
$(文件)。就绪(函数(){
$ .getJSON( GetProgressEvent,功能(数据){
$。每个(data.ProgressEvents,函数(){
$(#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>

Servlet GetProgressEvent.java:

  @覆盖
保护无效的doGet(HttpServletRequest的REQ,HttpServletResponse的RESP)
抛出的ServletException,IOException的
{
resp.setContentType( 应用程序/ 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();

}

我注意到servlet通常发送数据,发送的数据如下所示:

  {'ProgressEvents':[{'progress':10,'status':'10 %'},{'progress':20,'status':'20%'},{'progress':30,'status':'30%'}}]} 

但问题是循环从未执行,我不明白为什么。
我刚开始使用jQuery,我需要你的帮助,任何帮助都可以预先得到
的帮助:)

解决方案

问题是'是不是有效的json你需要双引号。

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

I want to create a progressbar that takes data from the server (servlet). So i create a html file and a servlet wich create json data(static for test purpose)

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

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

   }

with firebug I noticed that the servlet sends the data normally, the data sent lokk like this:

{'ProgressEvents':[{'progress':10,'status':'10%'},{'progress':20,'status':'20%'},{'progress':30,'status':'30%'}}]}

but the problem that the loop is never executed and i dont understand why. I just started with jQuery and I need your help,any help is apreciated thank you in advance for the help :)

解决方案

The problem is that ' is not valid json you need double quotes.

You can validate the json using http://jsonlint.com/

这篇关于JQuery从servlet获取JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆