I'm trying to retrieve the data from the database using the json object. But when i call the servlet, the Jquery will returns SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data This error only shows when the response contains more data.
My script is :
$.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);
},
});
And the response received..
{"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}}
Servlet code :
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());
}
Please anyone tell me how to solve this problem ... Thanks....
{...} {...}is not valid.{...}and[{...},{...}]would be. Fix your server code yo create valid JSON.