0

ユーザーがphp mysql jqueryとajaxを使用してJsonを使用してコメントを書き込むことができるコメント投稿システムを作成していますが、Jsonを挿入し、firedebugを使用してシステムをデバッグすると、次のエラーメッセージが表示されます : SyntaxError: JSON.parse : 予期しない文字がこのエラーを解決してくれる人はいますか?

これはコード@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: /");
  }


?>
4

2 に答える 2

0

2つのこと。1.解析しているデータに無効な文字があるかどうかを確認します。

  1. JSON オブジェクトのコレクションを取得していて、それらを解析してバインドしようとしている場合は、JSON オブジェクトを調べてビューにバインドする必要があります。
于 2013-03-31T19:11:05.353 に答える