JQuery Get JSON from servlet
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 voi开发者_开发问答d 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/
精彩评论