javascript - PHP,Jquery(ajax) post uncaught syntaxerror :unexpected token <

标签 javascript php jquery ajax

伙计们,我不断收到这个未捕获的语法错误:意外 token <,它指向 jquery 库文件,我正在使用 JQuery(comment-insert.js) 向 AJAX(comment-insert-ajax.php) 发出请求,我尝试在 AJAX 脚本中删除 php(?>) 的结束标记,但我仍然收到错误。我实际上在添加“require_once”行时收到错误,对此感到抱歉。

评论插入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: /');

}

以下是使用“$.post”方法发出请求的 Jquery 文件。 评论插入.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);
}

错误指向jQuery库的第7497行,它指向下面的代码

    jQuery.parseJSON = function(data)
{
  return data;
}

最佳答案

尝试使用 JSON.parse 函数:

//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);
}

关于javascript - PHP,Jquery(ajax) post uncaught syntaxerror :unexpected token <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887540/

相关文章:

javascript - 有没有办法将 JavaScript 注入(inject) WebView 以备后用?

javascript - 选择 href 为 ="#"的标签并打开父 li

javascript - 如何在 JavaScript 中为输入字段编写 mm/dd/yyyy 日期格式?

javascript - 如果购物车商品数量为三 – 隐藏添加到购物车 |店铺化

php - 如何查看我的登录页面中的cookie值?

php - 使用 PHP 进行正确且方便的错误处理和日志记录

javascript - 快速 Javascript 继承 : Understanding __proto__

javascript - antd: 'inputValue' Select 组件的 prop 不起作用

php - 如何使文本中的每个单词都可以点击并将其发送到脚本

javascript - 如何获取 JSON 并使用 jQuery 将其推送到 HTML 中?