【发布时间】:2017-03-10 09:03:35
【问题描述】:
这个让我恶心。因为我找不到哪里出错了。我会的 感谢您的任何帮助或提示。下面是我的javascript代码。到目前为止,服务器端很好,但在客户端显示实际评论是问题所在。请帮帮我。
$(document).ready(function () {
// process the form
$('form.comments_form').each(function () {
var form_to_submit = $(this);
form_to_submit.submit(function (event) {
event.preventDefault();
var posta_id = form_to_submit.find("input[type=hidden].UNIQUE_ID").val();
var tetxarea1 = form_to_submit.find("textarea.target").val();
$.ajax({
type: 'POST', // define the type of HTTP verb we want to use (POST for our form)
url: 'http://localhost/Forepost/php/real_time_comment.php', // the url where we want to POST
data: {
posta_id: posta_id,
tetxarea1: tetxarea1
}, // our data object
dataType: 'json', // what type of data do we expect back from the server
success: (function (response) {
display_the_comment(jQuery.parseJSON(response));
console.log(response);
}),
error: function () {
alert("oops something went wrong");
// oops something went wrong
}
});
//FUNCTION TO DISPLAY COMMENT FROM DATABASE
function display_the_comment(response) {
var comment_string = " ";
comment_string += "<li class='indiv_cmnts'>";
comment_string += "<span class='user_fname2'>'" + response.f_name + "'</span>";
comment_string += "<div class='my_msg'>'" + esponse.my_comment + "'</div>";
comment_string += "<img class='user_proff' src='" + response.profile_img + "'/>";
comment_string += "<span class='time_cmnts'>'" + response.my_comment_date + "'</span>";
//comment_string += "<span class='fa_reply'><i class='fa fa-reply' aria-hidden='true'></i> reply</span>";
comment_string += "</li>";
$("ul.comenting2").prepend(comment_string);
}
//FUNCTION TO DISPLAY COMMENT FROM DATABASE
});
});
});
我正在尝试将列表显示为具有“commenting2”类的无序列表
【问题讨论】:
-
您返回的 JSON 数据是什么?我最近遇到了这个问题,这是由于在 JSON 响应中发送了非法字符。
-
您能否尝试获取您用于解析的 JSON 响应字符串或数据?这将有助于找出问题。我的意思是我们需要使用 jQuery.parseJSON 解析的“响应”数据
-
你不需要使用 JSON.parse,因为你已经告诉 jquery 你期望 json 数据返回它会被自动解析并且你得到一个响应对象。运行 console.log(response) 而不事先解析,看看我的意思。
-
$my_comment = $rowR['cmets']; $my_comment_date = $rowR['date_time_commented']; $time = date("d-M-Y", strtotime($my_comment_date)); $std=新标准类(); $std->f_name=$f_name; $std->l_name=$l_name; $std->my_comment=$my_comment; $std->my_comment_date=$my_comment_date; $std->profile_img='localhost/Forepost/icons/forepost_user.png'; echo json_encode($std);
-
好的,这是我从我的 php 准备的数据
标签: javascript jquery html css json