2012-08-26 39 views
0

到目前为止,我的脚本中的一切似乎都很完美。但是通过Json插入和检索的数据没有被插入到我的div中。任何敏锐的眼睛都能看到任何问题。阿贾克斯不张贴到div

<div id="commentaddid"></div> 

AJAX

<script> 
    $(document).ready(function(){ 
    $("form#mycommentform").submit(function(event) { 
    event.preventDefault(); 
    var streamidcontent = $(this).children('#streamidcontent').val(); 
    var content = $(this).children('#content').val(); 

    $.ajax({ 
    type: "POST", 
    url: "comment_add.php", 
    cache: false, 
    dataType: "json", 
    data: { streamidcontent: streamidcontent, content: content}, 
    success: function(data){ 
    $('#commentaddid').append('<div class="stream_comment">"+content+"</div>'); 
    } 
    }); 
    return false 
    }); 
    }); 
    </script> 

MAIN STATUS AJAX我从工作。

<script> 
$(document).ready(function(){ 
$("form#myform").submit(function(event) { 
event.preventDefault(); 
var content = $("#toid").val(); 
var newmsg = $("#newmsg").val(); 

$.ajax({ 
type: "POST", 
url: "insert.php", 
cache: false, 
dataType: "json", 
data: { toid: content, newmsg: newmsg }, 
success: function(response){ 
$("#newmsg").val(""); 
$("#homestatusid").html("<div id='divider-"+response['streamitem_id']+"'><div class='userinfo'><a href='/profile.php?username="+response['username']+"'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><div class'delete' style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('"+response['streamitem_id']+"');\">X</div><a href='/profile.php?username="+response['username']+"'>"+response['first']+" "+ response['middle']+" "+response['last']+"</a><span class='subtleLink'> said</span><br/><a class='subtleLink' style='font-weight:normal;'>"+response['streamitem_timestamp']+"</a><hr>"+newmsg+"<div style='height:20px;' class='post_contextoptions'><div id='streamcomment'><a style='cursor:pointer;' id='commenttoggle_"+response['streamitem_id']+"' onclick=\"toggle_comments('comment_holder_"+response['streamitem_id']+"');clearTimeout(streamloop);swapcommentlabel(this.id);\">Write a comment...</a></div><div id='streamlike'><a title='Like "+response['first']+" "+ response['middle']+" "+response['last']+"s status' id='likecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"likestatus("+response['streamitem_id']+",this.id);\"><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'>Like</a></div><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'><a title='See who likes "+response['first']+" "+ response['middle']+" "+response['last']+"s status' href='include/likes.php?streamitem_id="+response['streamitem_id']+"' /></a></div></div></form></div><div id='streamdislike'><a id='dislikecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"dislikestatus("+response['streamitem_id']+",this.id);\"><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'>Dislike</a></div><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'></div></div></form><div class='stream_comment_holder' style='display:none;' id='comment_holder_"+response['streamitem_id']+"'><div id='comment_list_"+response['streamitem_id']+"'><table width=100%><tr><td valign=top width=30px><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><td valign=top align=left><div class='stream_comment_inputarea'><input id='addcomment' type='text' name='content' style='width:100%;' class='input_comment' placeholder='Write a comment...' onkeyup='growcommentinput(this);' autocomplete='off' onkeypress=\"if(event.keyCode==13){addcomment("+response['streamitem_id']+",this.value,'comment_list_"+response['streamitem_id']+"',"+response['id']+",'"+response['first']+" "+ response['middle']+" "+response['last']+"');this.value='';}\"><br/></div></div>"); 
} 
}); 
return false 
}); 
}); 
</script> 
+0

它通常是* *一个PHP *或*一个JavaScript问题。请再次验证PHP部分是否正常工作,因此您不必将其发布。 –

+0

...但是,请发布PHP脚本生成的示例JSON。这在调试某些问题时很有用。 – LSerni

回答

2

(注:也许你不想.appenddiv但更换div的内容)。

这段代码是错的 - 你不匹配的单引号和双引号:

$('#commentaddid').append('<div class="stream_comment">"+content+"</div>'); 

应该是:

$('#commentaddid').append('<div class="stream_comment">'+content+'</div>'); 

...实际上,应该不止于此。

data由JSON返回的值是一个复杂的对象,你应该格式(这将是审慎的PHP发出对JSON一个Content-Type太):

内容=“” +数据。名称+“评论:”+ ...

2

您获取数据的data,但要附加作为content(这实际上是要发送的数据)。更改您的代码

$('#commentaddid').append('<div class="stream_comment">'+data+'</div>'); 
+0

顺便说一句,你确定你想要追加原始的JSON吗,还是应该绕过它,比如在添加JSON数据之前创建一些HTML元素? – SexyBeast

+0

它的用户评论,添加到主帖子。评论应该向上并显示输入区域上方的最后评论。非这些答案正在工作。我已经尝试了其中的大部分。 – dave

+0

它不应该工作。您只是试图将JSON数据附加到'div',而实际上,您应该从JSON数组中获取数据,构建HTML元素,如'

'+ data.first +'

'',然后追加它。 – SexyBeast

0
$('#commentaddid').append('<div class="stream_comment">'+data.content+'</div>');