我正在尝试使用AXIOS Post方法从数据库中删除数据。
我在后端代码上创建了API删除功能,并使用POSTMAN进行了验证。使用POSTMAN,我可以在数据库中创建和删除数据。但是,在React Native上,当调用delete函数时,我收到“ 请求失败,状态码为404 ”。
下面,我从React Native创建/删除功能和后端创建/删除功能中添加了代码段。还提供了React本机控制台屏幕截图和应用程序界面屏幕截图。
本机创建和删除功能:
//Add date and time
addDateTimeAppt = () => {
let self = this;
AsyncStorage.getItem('my_token').then((keyValue) => {
console.log('Freelancer Create Screen (keyValue): ', keyValue);
axios({
method: 'post',
url: Constants.API_URL + 'appointment_f/create_appointment/',
data: {
app_date_start: self.state.textAppointmentDate,
start_time: self.state.textAppointmentTime,
end_time: self.state.textEndTime,
},
headers: {
'X-API-KEY': Constants.API_KEY,
'Authorization': keyValue,
},
}).then(function (response) {
self.setState({
timeSlots: [
...self.state.timeSlots,
{
apptdate: self.state.textAppointmentDate,
appttime: self.state.textAppointmentTime,
endTime: self.state.textEndTime,
}
],
});
console.log(response.data);
}).catch(function (error) {
console.log(error);
});
});
}
//delete date and time
deleteDateTimeApi = (id) => {
let self = this;
AsyncStorage.getItem('my_token').then((keyValue) => {
console.log('from delete: ', Constants.API_URL + 'appointment_f/delete_appointment/')
axios({
method: 'post',
url: Constants.API_URL + 'appointment_f/delete_appointment/',
data: {
app_date_start: self.state.textAppointmentDate,
start_time: self.state.textAppointmentTime,
end_time: self.state.textEndTime,
},
headers: {
'X-API-KEY': Constants.API_KEY,
'Authorization': keyValue,
'Content-type': 'application/json',
'Accept': 'application/json',
},
}).then(function (response) {
const filteredData = this.state.timeSlots.filter(item => item.id !== id);
this.setState({ timeSlots: filteredData });
console.log(response.data);
}).catch(function (error) {
console.log(error);
console.log(error.response);
});
});
}
后端创建和删除功能:
//Create Appointment using CodeIgniter
public function create_appointment_post(){
$save['freelancer_id'] = $this->user->comp;
$start = new DateTime($this->post('app_date') . ' ' . $this->post('start_time'));
$end = new DateTime($this->post('app_date') . ' ' . $this->post('end_time'));
$save['app_date_start'] = $start->format('Y-m-d H:i:s');
$save['app_date_end'] = $end->format('Y-m-d H:i:s');
$this->freelancer_timeslot_model->save($save);
$this->response($save, REST_Controller::HTTP_OK);
}
//Delete Appointment using CodeIgniter
public function delete_appointment_post(){
$f_id = $this->post('freelancer_id');
$app_date_start = $this->post('app_date_start');
$app_date_end = $this->post('app_date_end');
$timeslot = $this->freelancer_timeslot_model->get_by(
array(
'freelancer_id' => $f_id,
'app_date_start' => $app_date_start,
'app_date_end' => $app_date_end,
), TRUE);
if(!empty($timeslot)){
$this->freelancer_timeslot_model->delete($timeslot->id);
$this->response(['status' => TRUE], REST_Controller::HTTP_OK);
}else {
$this->response([
'status' => FALSE,
'message' => 'No record found'
], REST_Controller::HTTP_NOT_FOUND);
}
}
反应本机Console.log():
应用程序界面: