我有一个功能连接onclick按钮。 它通过ajax发送一些数据到另一个php文件。 其实一切工作正常,但我试图现在添加到'成功'一个简单的IF语句,这将从外部文件得到真或假。 我想这里的一些语法可能是错误的,因为错误消息说我的主要功能没有定义。 请问有人可以在这里找到一些错误吗?语法困难在ajax
主文件(在这里取得成功:我已经添加的if else语句只):
$.ajax({
type: 'get',
url: '/editcomment',
data: {newComment: newComment,
id: id,
userId: userId},
success:function(data){
if(data.success){
$(".newCommentForm" +id).replaceWith("<span id='" + id + "' class='limitedText'>" + newComment + "</span>");
$(thisId).data("PinComment", newComment);
}
else { alert('error');},
error:function(){
alert('error');
}
});
}
外部文件(我这里补充返回数组):
public function editComment(){
$userId = Input::get('userId');
if ($userId == Auth::id() or Auth::user()->hasRole("admin")){
$id = Input::get('id');
$newComment = Input::get('newComment');
DB::update('UPDATE `comments` SET f_text = ? WHERE h_id = ?', array($newComment, $id));
return array('success' => true);
} else {
return array('success' => false);
}
}
你定的代码和说明不清晰,完整 –
这是很难解释整个项目:DI只是有一些语法问题在阿贾克斯 – UberProCoder
即使这个简单的返回数据,我建议你使用'echo json_encode(array('success'=> false));'并将数据作为json字符串返回。它使一切都更容易在JavaScript中使用。还要在''.ajax'参数列表中添加'dataType:'json''。 – RiggsFolly