[英]Please check the code and give me why there is this error?
this is my servlet code这是我的 servlet 代码
List<Group> list= dao.findgroup(user);
JSONObject json=(JSONObject) JSONSerializer.toJSON(list);
ServletResponse response=ActionContext.getServletResponse();
response.setContentType("text/JSON");
PrintWriter out=response.getWriter();
out.println(json);
out.close();
//this is my jquery code //这是我的jquery代码
$.get("viewgroup.process",function(data){
var use=$.parseJSON(data);
$(use).each(function(i,v)
{
var det="<tr><td>"+v.value+"</td><td>"+v.description+"</td><td>"+v.code+"</td><td>"+v.status+"</td><td><a href='#'>reset code</a></td><td><a href='#'>change status</a></td></tr>";
$(det).appendTo("#tablebody");
});
Now my problem is when i am sending this request and getting a list as json object,and when I use method parseJSON it gives me error:现在我的问题是当我发送此请求并获取列表作为 json 对象时,当我使用方法 parseJSON 时,它给了我错误:
SyntaxError: JSON.parse: unexpected character
Can any one tell me why this error is there?谁能告诉我为什么会出现这个错误?
Pretty sure the data
parameter when using $.get
is already a javascript object (rather than a JSON string), so no need to parse it again:很确定使用
$.get
时的data
参数已经是一个 javascript 对象(而不是一个 JSON 字符串),所以不需要再次解析它:
$.get("viewgroup.process",function(data){
var use = data;//or just use data directly rather that a new variable called use
$(use).each(function(i,v)
{
var det="<tr><td>"+v.value+"</td><td>"+v.description+"</td><td>"+v.code+"</td><td>"+v.status+"</td><td><a href='#'>reset code</a></td><td><a href='#'>change status</a></td></tr>";
$(det).appendTo("#tablebody");
});
//rest of code...
});
Change out.println(json);
更改
out.println(json);
to out.println(json.toString());
到
out.println(json.toString());
in your servlet code.在您的 servlet 代码中。 You want to send out the stringified JSON, not the actual object.
您想发送字符串化的 JSON,而不是实际的对象。
http://www.json.org/javadoc/org/json/JSONObject.html#toString%28%29 http://www.json.org/javadoc/org/json/JSONObject.html#toString%28%29
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.