2013-03-31 63 views
0

我正在创建一个评论帖子系统,允许用户使用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: /"); 
    } 


?> 
+0

后很可能从'ajax/comment_insert.php'返回的响应不是有效的json。请告诉我们输出结果如何, –

+0

你在php中使用json_encode吗? jQuery严格分析JSON数据。尝试使用json_encode,它总是与jQuery一起工作 – alasarr

+0

我编辑我的问题,是的,我正在使用json_encode – user2214618

回答

0

两件事。 1.查看您正在解析的数据是否有一些无效的文字记录器。

  1. 如果您正在获取JSON对象的集合,并试图通过它们进行解析并绑定它们,则应查看JSON对象并将其绑定到视图。
+0

在火灾调试的例子,我得到:** SyntaxError:JSON.parse:意外的字符**在第3行jquery.js这是juqery文件 – user2214618

0
if(class_exists('Comments')) 
    { 
     $commentInfo = Comments::insert($comment,$userId); 

     if($commentInfo!=null) 
     { 

     } 
    } 

我想插入函数接受2个变量,如commentuserid 但你提到的一个变量$comment.$userid

,我认为这是问题,一旦执行代码的改变. as ,