1

オブジェクトを使用してデータベースからデータを取得しようとしていjsonます。しかし、サーブレットを呼び出すと、Jqueryはこのエラーを返しますSyntaxError: JSON.parse: unexpected non-whitespace character after JSON data。このエラーは、応答にさらにデータが含まれている場合にのみ表示されます。

私のスクリプトは:

    $.ajax({
       type: "GET",
        url: "VComment",
        data:'comm='+encodeURIComponent(comm)+'&'+'data-id='+encodeURIComponent(dataid)+'&'+'data-alid='+encodeURIComponent(dataalid),
        dataType: "json",
        success: function( data, textStatus, jqXHR) 
        {
            if(data.success)
            {
                    var newcommhtml = '<div id="c0'+thecid+'" class="cnew clearfix"> <section class="c-author">';
                    newcommhtml = newcommhtml + '<h3>Anonymous</h3>';
                    newcommhtml = newcommhtml + '<span class="pubdate">'+month+' '+day+', '+year+'</span> </section>';
                    newcommhtml = newcommhtml + '<section class="c-content">';
                    newcommhtml = newcommhtml + '<img src="images/green-avatar.png" alt="avatar" width="80" height="80" class="ava">';
                    newcommhtml = newcommhtml + '<p>'+nl2br(data.commentInfo.comment)+'</p> </section></div>';

                    var thelm = "#c0"+thecid;
                    commwrap.append(newcommhtml);
                    $(thelm).hide().fadeIn('slow');

                    setTimeout(function() { $(thelm).addClass('green'); }, 800);

                    $("#comm").val("");
                    thecid++;

                    if(errorspan.html() != null) {
                        errorspan.remove();
                    }
            }

          },
     error: function(jqXHR, textStatus, errorThrown)
      {
         alert("error"+errorThrown);
         console.log("Something really bad happened " + textStatus);
      },
});

そして、受け取った応答。

    {"success":true,"commentInfo":{"uname":"shyam","comment":"rreter","itemId":0}}
    {"success":true,"commentInfo":{"uname":"shyam","comment":"dfdsfdd","itemId":0}}
    {"success":true,"commentInfo":{"uname":"shyam","comment":"xzdfdsfdd","itemId":0}}
    {"success":true,"commentInfo":{"uname":"shyam","comment":"sdfsd fsdfs","itemId":0}}
    {"success":true,"commentInfo":{"uname":"shyam","comment":"sdsd","itemId":0}}
    {"success":true,"commentInfo":{"uname":"shyam","comment":"dd","itemId":0}}
    {"success":true,"commentInfo":{"uname":"shyam","comment":"dddf","itemId":0}}

サーブレットコード:

      while(rs.next()){
            Commenter comment = new Commenter();
            comment.setUname(rs.getString("uname").trim());
            comment.setComment(rs.getString("comments").trim());
            commentObj=gson.toJsonTree(comment);
            myObj.add("commentInfo", commentObj);
            out.println(myObj.toString());
            }   

誰かがこの問題を解決する方法を教えてください...ありがとう....

4

3 に答える 3

3

応答には、いくつかの独立したJSONオブジェクトがあります。それらを配列で包むと、あなたはより良くなるでしょう

[{"success":true,"commentInfo":{"uname":"shyam","comment":"rreter","itemId":0}},
{"success":true,"commentInfo":{"uname":"shyam","comment":"dfdsfdd","itemId":0}},
{"success":true,"commentInfo":{"uname":"shyam","comment":"xzdfdsfdd","itemId":0}},
{"success":true,"commentInfo":{"uname":"shyam","comment":"sdfsd fsdfs","itemId":0}},
{"success":true,"commentInfo":{"uname":"shyam","comment":"sdsd","itemId":0}},
{"success":true,"commentInfo":{"uname":"shyam","comment":"dd","itemId":0}},
{"success":true,"commentInfo":{"uname":"shyam","comment":"dddf","itemId":0}}]

関数を次のように変更します。

success: function( data, textStatus, jqXHR) 
    {
       for(var i = 0,len=data.length;i<len;i += 1){
        if(data[i].success)
        {
          //code
        }
       }
    }

サーバー側で次のように変更します

out.println("[");
Boolean first = true
while(rs.next()){
        Commenter comment = new Commenter();
        comment.setUname(rs.getString("uname").trim());
        comment.setComment(rs.getString("comments").trim());
        commentObj=gson.toJsonTree(comment);
        myObj.add("commentInfo", commentObj);
        if(!first){
          out.print(",");
        } else {
          first = false;
        }
        out.println(myObj.toString());
        }   
out.println("]");
于 2013-02-21T13:31:26.847 に答える
1

このコードを試してみてくださいそれはあなたの問題を解決するかもしれないと思います:

        ArrayList<JSONObject> CommArray=new ArrayList<JSONObject>();

         while(rs.next()){
            JSONObject Obj = new JSONObject();
            Obj.put("uname",rs.getString("uname").trim());     //Adds your uname to Object
            Obj.put("comment",rs.getString("comments").trim());//Adds your comment to Object
            CommArray.add(Obj);                                //Inserts your Object to ArrayList
            System.out.println(rs.getString("comments").trim());
            }    
         JSONArray arrayObj=JSONArray.fromObject(CommArray);//Converts the Array List to JSONArray
         commentObj=gson.toJsonTree(arrayObj);   //Converts the JSONArray to Jsontree
         myObj.add("commentInfo", commentObj);   //Adds the Tree to JsonObject as commentInfo Array
         out.println(myObj.toString());        //Prints the result
         rs.close();                                                              
         stmt.close();                                                            
         stmt = null;                                                             
         conn.close();                                                            
         conn = null;                                                  

     }                                                              
     catch(Exception e){}

これであなたの問題が解決することを願っています。

于 2013-02-21T18:51:30.473 に答える
0

を一時的に変更しdataTypeて同じ悪い要求を行い、応答を出力してみてください。何が悪いのか見てみましょう。

于 2013-02-21T13:32:56.717 に答える