2

guys i keep on getting this uncaught syntaxerror:unexpected token <, and it points to the jquery library file,i am making a request to AJAX(comment-insert-ajax.php) using JQuery(comment-insert.js),i try removing the closing token of php(?>) in the AJAX script but i still get the error.I actually get the error when i add the 'require_once' lines,sorry about that.

comment-insert-ajax.php

    <?php 

if(isset($_POST['task']) && $_POST['task'] == 'comment-insert')
{

    $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 = "Thabo Ambrose";
    $std->profile_img= "images/tbo.jpg";

    require_once $_SERVER['DOCUMENT_ROOT'] . 'defines.php';
   require_once MODELS_DIR . 'Comments.php'; 
    echo json_encode($std);


}
else
{
        header('location: /');

}

and following is the Jquery file that makes the request using the '$.post' method. comment-insert.js

$(document).ready(function(){

   $('#comment-post-btn').click(function(){

        comment_post_btn_click();

    });


});


function comment_post_btn_click()
{

        var _comment = $('#comment-post-text').val();
        var _userId = $('#user-id').val();
        var _userName = $('#user-name').val();

        if(_comment.length > 0 && _userId != null)
        {
            //proceed with ajax call back
            $.post("ajax/comment-insert-ajax.php",
            {
                    task : "comment-insert",
                    userId : _userId,
                    comment : _comment
            }

            ).error(

                    function()
                    {
                        console.log("Error : ");
                    }
            ).success(

                    function(data)
                    {
                        comment_insert(jQuery.parseJSON(data));
                        console.log("Response text : "+ data);
                    }
            );


            console.log(_comment + " "+_userName + " id of "+_userId);  
        }
        else
        {

                //do something

                $('#comment-post-text').addClass('alert alert-danger');
                $('#comment-post-text').focus(function(){$('#comment-post-text').removeClass('alert alert-danger');});
        }   

        //remove text in the text area after posting
        $('#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);
}

The error points to line 7497 of the jQuery library,it point to the following code

    jQuery.parseJSON = function(data)
{
  return data;
}
Thabo
  • 1,303
  • 2
  • 19
  • 40
  • What you get in `data` in success block? – AnkiiG Nov 24 '15 at 07:32
  • I parse the data from the ajax response using the jquery.parse (data), buy since there is this error i get nothing – Thabo Nov 24 '15 at 07:36
  • Will you comment `comment_insert(jQuery.parseJSON(data));` and paste what you get in `data`? – AnkiiG Nov 24 '15 at 07:37
  • try removing `header('location: /');` in your else block and echo something else there and inspect the response with chrome Inspector or Firebug. – abhishekkannojia Nov 24 '15 at 07:38
  • might be worth trying `json_encode( $std, JSON_HEX_TAG );` – Professor Abronsius Nov 24 '15 at 07:39
  • Probably you are throwing a PHP Notice or Warning. Could you look to the AJAX call response and paste it here? You can do it with the developer tools of any browser. – artberri Nov 24 '15 at 07:51
  • i get this from the success {"comment_id":24,"userId":1,"comment":"FGH","userName":"Thabo Ambrose","profile_img":"images\/tbo.jpg"} – Thabo Nov 24 '15 at 07:53
  • i have edited the question please check, i only get the error when i use the 'require_once' clause in the ajax file. – Thabo Nov 24 '15 at 07:54
  • I still think that the PHP is throwing a warning or error, even more if you said that you only get the error when you add the require lines. There is no PHP error printed before that? This response is when you get the error? – artberri Nov 24 '15 at 07:58
  • No the uncaught syntax error is the only error i am getting, – Thabo Nov 24 '15 at 07:59

3 Answers3

1

Try using the JSON.parse function:

//proceed with ajax call back
    $.post("ajax/comment-insert-ajax.php",
        {
            task : "comment-insert",
            userId : _userId,
            comment : _comment
        }

    ).error(

        function()
        {
            console.log("Error : ");
        }
    ).success(

        function(data)
        {
            comment_insert(JSON.parse(data));
            console.log("Response text : "+ data);
        }
    );


    console.log(_comment + " "+_userName + " id of "+_userId);
}
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
Ilanus
  • 6,690
  • 5
  • 13
  • 37
0

try puting the data in a array and then encode it

$array = ['comment_id' => 24,'userId' => $userId,'comment' => $comment,'userName' => "Thabo Ambrose",'profile_img'= "images/tbo.jpg"];


echo json_encode($array);
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
-1

change if(isset($_POST['task']) && $_POST['task'] == 'comment-insert') this line of code to if(isset($_POST['task']) && isset($_POST['task'])&&$_POST['task']== 'comment-insert'). then change the ajax call to

    $.ajax({
                        url: "ajax/comment-insert-ajax.php",
                        data :{"task":"comment-insert","UserId":_userId,"comment":_comment},
                        type: "POST",       
                        dataType: "json",
    success:function(msg) { 
    }
});
Jasmel Pc
  • 515
  • 1
  • 5
  • 16