Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PHP,Jquery(ajax)post uncaught语法错误:意外标记<;_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript PHP,Jquery(ajax)post uncaught语法错误:意外标记<;

Javascript PHP,Jquery(ajax)post uncaught语法错误:意外标记<;,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,伙计们,我一直在AJAX脚本中得到这个未捕获的语法错误(syntaxerror:unexpected token),但我仍然得到了错误。我实际上在添加“require_once”行时得到了错误,对此表示抱歉 comment-insert-ajax.php <?php if(isset($_POST['task']) && $_POST['task'] == 'comment-insert') { $userId = (int)$_POST['userI

伙计们,我一直在AJAX脚本中得到这个未捕获的语法错误(syntaxerror:unexpected token),但我仍然得到了错误。我实际上在添加“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: /');

}

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

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

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


echo json_encode($array);
if(isset($\u POST['task'])和&$\u POST['task']=='comment insert')
这行代码更改为
if(isset($\u POST['task'])和&isset($\u POST['task'])&&&$\u 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) { 
    }
});

在成功块中的
data
中得到了什么?我使用jquery.parse(data)解析ajax响应中的数据,因为存在此错误,所以我什么也没有得到。您会对
comment_insert(jquery.parseJSON(data))进行注释吗并粘贴在
数据中得到的内容
?尝试删除
标题('位置:/')在你的else块中,在那里回显其他内容,并使用chrome Inspector或Firebug检查响应。可能值得尝试
json\u encode($std,json\u HEX\u标记)
JSON.parse()
jQuery.parseJSON
执行相同的操作。
$array = ['comment_id' => 24,'userId' => $userId,'comment' => $comment,'userName' => "Thabo Ambrose",'profile_img'= "images/tbo.jpg"];


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