我无法弄清楚为什么我会收到错误" JSON.parse:意外字符"在Firefox控制台..看起来问题是围绕jQuery.parseJSON(数据));在comment.js文件..如果我删除我的PHP文件中的两个if语句一切正常,但这不是我需要的..
谢谢!
comment.php
<?php
require_once ("../modules/comments.php");
require_once ("../includes/db_connect.php");
if(isset($_POST['task']) && $_POST['task'] == 'comment_insert' ) {
$userId = (int)$_POST['userId'];
//$comment = addslashes(str_replace("\n", "<br>", $_POST['comment']));
$comment = strip_tags($_POST['comment']); // remove html tags
$comment = ucfirst(strtolower($comment));
//$comment = addslashes(str_replace("\n", "<br>"));
$std = new stdClass();
$std->commentId = 24;
$std->userId = $userId;
$std->comment = $comment;
$std->userName = "John Stu";
$std->profile_img = "https://s.ytimg.com/yts/img/avatar_48-vfllY0UTT.png";
if(class_exists('Comments') && class_exists('Subscribers')) {
$commentInfo = Comments::insertComments($comment, $userId);
if($commentInfo != null) {
}
}
echo json_encode($std);
} else {
//header("Location: /");
}
?>
comment.js
$(document).ready(function(){
$('#post-comment-btn').click(function(){
comment_post_btn_click();
});
});
function comment_post_btn_click(){
var _comment = $('#comment-post-text').val();
var _userId = $('#userId').val();
var _userName = $('#userName').val();
if(_comment.length > 0 && _userId != null) {
$.post("ajax/comment_insert.php", {
task : "comment_insert",
userId : _userId,
comment : _comment,
}).done(function(data) {
comment_insert(jQuery.parseJSON(data));
console.log("ResponseText:" + data);
});
console.log(_comment + " Username: " + _userName + " User Id: " + _userId);
} else {
console.log("The text area is empty..");
}
$('#comment-post-text').val("");
}
function comment_insert(data) {
var t = '';
t += '<li class="comment-holder" id="_'+data.comment_id+'">';
t += '<div class="user-img">';
t += '<img src="'+data.profile_img+'">';
t += '</div>';
t += '<div class="comment-body">';
t += '<h3 class="username-field">'+data.userName+'</h3>';
t += '<div class="comment-text">'+data.commen+'</div>';
t += '</div>';
t += '<div class="comment-buttons-holder">';
t += '<ul>';
t += '<li class="delete-btn">X</li>';
t += '</ul>';
t += '</div>';
t += '</li>';
$('.comments-holder-ul').prepend(t);
}
答案 0 :(得分:2)
您的PHP文件中看起来有一些错误/警告。
试试这个,
Inspect > Network >Select XHR
并进行AJAX调用。
现在,您将在Response
标签中看到PHP响应。
(更好地使用谷歌浏览器)
因为PHP错误/警告包含了从"<"
开始的HTML标记。
(示例<div>Undefined Index</div>
)
答案 1 :(得分:0)
首先清理输出缓冲区,然后终止当前脚本。尝试以下代码:
<强> comment.php 强>
require_once ("../modules/comments.php");
require_once ("../includes/db_connect.php");
if(isset($_POST['task']) && $_POST['task'] == 'comment_insert' ) {
$userId = (int)$_POST['userId'];
//$comment = addslashes(str_replace("\n", "<br>", $_POST['comment']));
$comment = strip_tags($_POST['comment']); // remove html tags
$comment = ucfirst(strtolower($comment));
//$comment = addslashes(str_replace("\n", "<br>"));
$std = new stdClass();
$std->commentId = 24;
$std->userId = $userId;
$std->comment = $comment;
$std->userName = "John Stu";
$std->profile_img = "https://s.ytimg.com/yts/img/avatar_48-vfllY0UTT.png";
if(class_exists('Comments') && class_exists('Subscribers')) {
$commentInfo = Comments::insertComments($comment, $userId);
if($commentInfo != null) {
}
}
// Clean the output buffer
ob_clean();
echo json_encode($std);
// Terminate the current script
die();
} else {
//header("Location: /");
}
?>
答案 2 :(得分:0)
如果要返回json数据,则必须在添加最后一个参数时将其指示给$ .post函数(否则,data
将不是json对象)...
$.post("ajax/comment_insert.php", {
task : "comment_insert",
userId : _userId,
comment : _comment,
},,"json").done(...
更改并检查错误是否仍然存在......