简体   繁体   English

请检查代码并告诉我为什么会出现此错误?

[英]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.

相关问题 为什么这段代码给我使用Interface的编译时错误,以及如何实现此代码? 请解释? - Why this code give me compile Time error using Interface and how i can implement this code? Please Explain? 有人可以检查为什么这给了我一个错误 - Can someone please check why this is giving me an error 请告诉我为什么下面的代码不起作用? - Please tell me why the code below is not working? 运行时错误不幸的是,应用已停止,请给我解决方案 - Runtime Error Unfortunately app has stopped please give me solution 它不会在代码MySQLSyntaxErrorException中给我错误 - It does not give me error in code, MySQLSyntaxErrorException 为什么命令提示符在编译时给我一个错误代码,而我的 IDE 却没有? - Why does command prompt give me an error code in compilation when my IDE doesn't? 有人可以给我一个如何同时实现 DTO 和 DAO 概念的代码示例吗? - Can somebody please give me a code example of how to implement the DTO and DAO concept at the same time? 我不知道为什么会出现此错误,请帮助我 - I dont know why this error occurs, please help me 运行时错误。 请让我了解我做错了什么 - Runtime Error. Please give me understanding about what I am doing wrong 为什么声明一个新对象会给我一个语法错误? - Why does declaring a new object give me a syntax error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM