JSON オブジェクトを使用して、データベースから取得したデータを Web ページに渡そうとしています。しかし、私の問題は、
- データベースにさらにデータが含まれている場合、値を配列で渡す必要があります
- 値を配列で渡すとき。Jquery は結果を として返します
undefined
。
私のサーブレットコードは次のとおりです。
String message=request.getParameter("comm");
int itemId=Integer.parseInt(request.getParameter("data-id"));
int albumId=Integer.parseInt(request.getParameter("data-alid"));
PrintWriter out = response.getWriter();
response.setContentType("text/html");
response.setHeader("Cache-control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "-1");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
response.setHeader("Access-Control-Max-Age", "86400");
Gson gson = new Gson();
JsonObject myObj = new JsonObject();
ArrayList<Commenter> commentInfo;
try {
commentInfo = commenting(uname,message,itemId,albumId);
java.lang.reflect.Type listOfTestObject = new TypeToken<ArrayList<Commenter>>(){}.getType();
String str = gson.toJson(commentInfo, listOfTestObject);
JsonElement commentObj = gson.toJsonTree(str);
boolean nonNullElemExist= true;
for (Commenter s: commentInfo) {
if (s == null) {
nonNullElemExist = false;
break;
}
}
if(nonNullElemExist == false){
myObj.addProperty("success", false);
}
else {
myObj.addProperty("success", true);
}
myObj.add("commentInfo", commentObj);
out.println(myObj.toString());
out.close();
} catch (ClassNotFoundException | SQLException e) {
System.out.println( "Error --> " + displayErrorForWeb(e));
}
}
private ArrayList<Commenter> commenting(String uname,String message,int itemId,int albumId) throws ClassNotFoundException, SQLException {
ArrayList<Commenter> commentList = new ArrayList<Commenter>();
Connection conn = null;
conn=prepareConnection();
PreparedStatement stmt = null;
String sql = null;
try {
StringBuilder sb1=new StringBuilder(1024);
sb1.append("insert into ").append(uname.trim()).append("comments values(?,?,?)");
String sql1=sb1.toString();
PreparedStatement stmt1 = conn.prepareStatement(sql1);
stmt1.setString(1,uname);
stmt1.setString(2,message);
stmt1.setInt(3,itemId);
stmt1.executeUpdate();
StringBuilder sb=new StringBuilder(1024);
sb.append("select * from ").append(uname.trim()).append("comments");
sql=sb.toString();
stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
Commenter comment = new Commenter();
comment.setUname(rs.getString("uname").trim());
comment.setComment(rs.getString("comment").trim());
commentList.add(comment);
}
rs.close();
stmt.close();
stmt = null;
conn.close();
conn = null;
}
catch(Exception e){System.out.println( "Error --> " + displayErrorForWeb(e));}
finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlex) {
System.out.println( "Error --> " + displayErrorForWeb(sqlex));
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException sqlex) {
System.out.println( "Error --> " + displayErrorForWeb(sqlex));
}
conn = null;
}
}
return commentList;
}
そしてjqueryは次のとおりです。
$.ajax({
type: "GET",
url: "Comments",
data:'comm='+encodeURIComponent(comm)+'&'+'data-id='+encodeURIComponent(dataid)+'&'+'data-alid='+encodeURIComponent(dataalid),
dataType: "json",
success: function( data, textStatus, jqXHR)
{
if(data.success)
{
/* $.each(data, function(i, item)
{*/
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)+' '+nl2br(data.commentInfo.itemId)+'</p> </section></div>';
/*newcommhtml = newcommhtml + '<p>'+nl2br(item.commentInfo.comment)+' '+nl2br(item.commentInfo.itemId)+'</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);
},
});
$each()
success
ブロックを使用すると、まったく機能しませんでした。
誰かがこの問題を解決するのを手伝ってください....ありがとう..............