Javascript PHP,Jquery(ajax)post uncaught语法错误:意外标记<;
伙计们,我一直在AJAX脚本中得到这个未捕获的语法错误(syntaxerror:unexpected token),但我仍然得到了错误。我实际上在添加“require_once”行时得到了错误,对此表示抱歉 comment-insert-ajax.phpJavascript 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
<?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) {
}
});