对于Codeigniter上的AJAX端点,我的if-else条件如下 -
if ($this->db->insert('sets', $data)) {
$arr = array('status' => 'success', 'result' => $data);
echo json_encode($arr);
return;
} else {
echo json_encode(array('status' => 'error', 'exception' => 'cannot insert given data to db');
return;
}
按预期工作,返回成功数据并相应地更新视图。
但对于PK约束的db-> insert()失败(e,g故意插入与PK [重复录入]相矛盾的值);我从未接收过有关成功的数据。将失败回调添加到ajax后,我发现CI3导致500而不是200。 因此,在我看来,没有提供我在ajax回调中所期待的json。
$http.get(url, {})
.then(function successCb(data) {
if (data.data.status == 'error') {
$scope.addExceptionMsg = data.data.exception;
$("#errorPopup").modal(); // show the error popup
} else {
glueFactory.updateSet(data.data.result);
}
}, function errorCb(response) {
console.log(response); // <== I should not be here even on db insert failure.
});
如何在仍使用查询构建器时解决此问题?