【问题标题】:PHP,Jquery(ajax) post uncaught syntaxerror:unexpected token <PHP,Jquery(ajax)发布未捕获的语法错误:意外令牌<
【发布时间】:2015-11-24 07:17:38
【问题描述】:

伙计们,我不断收到这个未捕获的语法错误:意外令牌 ) 的关闭标记,但我仍然收到错误。当我添加“require_once”行时,我实际上收到了错误,对此感到抱歉。

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: /');

}

以下是使用“$.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;
}

【问题讨论】:

  • 你在成功块中的data得到了什么?
  • 我使用 jquery.parse (data) 解析来自 ajax 响应的数据,买因为有这个错误我什么都得不到
  • 你会评论comment_insert(jQuery.parseJSON(data)); 并将你得到的内容粘贴到data 吗?
  • 尝试删除 else 块中的 header('location: /'); 并在其中回显其他内容并使用 chrome Inspector 或 Firebug 检查响应。
  • 可能值得一试json_encode( $std, JSON_HEX_TAG );

标签: javascript php jquery ajax


【解决方案1】:

尝试使用 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);
}

【讨论】:

【解决方案2】:

尝试将数据放入数组中,然后对其进行编码

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


echo json_encode($array);

【讨论】:

    【解决方案3】:

    if(isset($_POST['task']) &amp;&amp; $_POST['task'] == 'comment-insert')这行代码更改为if(isset($_POST['task']) &amp;&amp; isset($_POST['task'])&amp;&amp;$_POST['task']== 'comment-insert')。 然后将ajax调用更改为

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 2014-02-09
      • 2011-04-07
      • 2014-05-09
      • 2014-11-24
      相关资源
      最近更新 更多