【发布时间】:2019-09-17 13:49:21
【问题描述】:
我在 codeigniter 上使用 rest api 时遇到问题,我的 api 控制器上有以下代码:
public function index_post()
{
$input = $this->input->post();
$data = array();
try {
$this->db->insert('attorneys', $input);
$data = array('id' => $this->db->insert_id(),
'message' => 'Attorney has been added');
$this->response($data, REST_Controller::HTTP_OK);
} catch (Exception $e) {
$data = array('error_number' => $this->db->_error_number());
$this->response($data, REST_Controller::HTTP_OK);
}
}
当我在数据库中遇到重复条目时收到错误,因为字段设置为唯一,我收到一个对象作为来自我的 ajax 发布请求的响应:
$.ajax({
type: "POST",
url: "/"+location.pathname.split('/')[1]+"/api/attorneys",
data: data,
dataType: "json",
success: function(data)
{
console.log('success');
console.log(data);
$("#attorneyList").append('<div class="list-info">'+
'<label><b>'+$('#inputAttorneyName').val()+'</b></label><br>'+
'<label>Law Office: <span>'+$('#inputLawOffice').val()+'</span></label><br>'+
'<label>Phone: <span>'+$('#inputAttorneyPhone').val()+'</span></label><br>'+
'<label>Email: <span>'+$('#inputAttorneyEmail').val()+'</span></label><br>'+
'</div>');
$('#attorneyIDs').val($('#attorneyIDs').val()+data.id+",");
$('#addAttorneyModal').modal('hide');
},
error: function(data) {
console.log('error');
console.log(data);
}
});
【问题讨论】:
标签: ajax rest api codeigniter