我想知道如果我已经使用了“if-else”语句,是否需要在下面提到的示例中使用ajax调用错误处理函数:
我的模型:
if ($query && $query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
我的 Controller :
if ($this->model_users->did_get_data($user_id)) {
$data["results"]= $this->model_users->did_get_data($user_id);
echo json_encode($data);
}
else {
$data = array(
'message' => "Could not find any posts."
);
echo json_encode($data);
}
我的JS文件:
$.get('controller/get_data', function (data) {
if (data.message !== undefined) {
$( "#data_is_not_got").text(data.message);
} else {
//displaying posts
}
}, "json");
最佳答案
是的,您仍然应该向 AJAX 调用添加错误处理程序。这将使您能够优雅地处理 if/else 语句未涵盖的调用中可能出现的任何潜在问题 - 无法连接到服务器、内部服务器错误等。
关于javascript - ajax调用错误处理函数或if-else语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25671644/