我正在創建一個評論帖子系統,允許用戶使用php mysql jquery和ajax編寫註釋並使用Json ,但是當我插入Json時我得到的問題和我使用firedebug調試系統時出現的問題我得到錯誤消息:語法錯誤:JSON.parse:意外的字符誰能幫我這個錯誤php mysql errors
這是代碼@line 42我使用JSON
$(document).ready(function() {
//this will fire whene the page will completly been loaded
$('#comment-post-btn').click(function(){
comment_post_btn_click();
});
});
function comment_post_btn_click()
{
// text within the text area
var _comment = $('#comment-post-text').val();
var _userId = $('#userId').val();
var _userName = $('#userName').val();
if(_comment.length > 0 && _userId != null)
{
// proceed with the ajax callback
$('#comment-post-text').css('border', '1px solid #e1e1e1');
$.post("ajax/comment_insert.php",
{
task : "comment_insert",
userId :_userId,
comment :_comment
}
)
.error(
function(data)
{
console.log("Error");
})
.success(
function(data)
{
comment_insert(jQuery.parseJSON(data));
console.log("Response text: " + data)
}
);
console.log(_comment + " UserName: " + _userName + " User id: " + _userId);
}
else
{
$('#comment-post-text').css('border', '1px solid #ff0000');
console.log("the text area was empty");
}
// remove the text from the textarea
$('#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+'" class="user-img-pic" />';
t+= '</div>';
t+= '<div class="comment-body">';
t+= '<h3 class="username-field">'+data.userName+'</h3>';
t+= '<div class="comment-text">'+data.comment+'</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);
}
comment_insert.php
<?php
if(isset($_POST['task']) && $_POST['task'] == 'comment_insert')
{
require_once("db_connect.php");
$userId = (int)$_POST['userId'];
$comment = addslashes(str_replace("\n", "<br>", $_POST['comment']));
$std = new stdClass();
$std->comment_id = 24;
$std->userId = $userId;
$std->comment = $comment;
$std->userName = "georges matta";
$std->profile_img = "images/default_img.jpg";
require_once("comments.php");
if(class_exists('Comments'))
{
$commentInfo = Comments::insert($comment.$userId);
if($commentInfo!=null)
{
}
}
**echo json_encode($std);**
}
else
{
header("Location: /");
}
?>
後很可能從'ajax/comment_insert.php'返回的響應不是有效的json。請告訴我們輸出結果如何, –
你在php中使用json_encode嗎? jQuery嚴格分析JSON數據。嘗試使用json_encode,它總是與jQuery一起工作 – alasarr
我編輯我的問題,是的,我正在使用json_encode – user2214618