Javascript React本机Axios请求失败,状态代码404
我正在尝试使用AXIOS Post方法从数据库中删除数据 我在后端代码上创建了一个API delete函数,并使用POSTMAN进行验证。使用POSTMAN,我可以从数据库中创建和删除数据。然而,在React Native上,当调用delete函数时,我得到“请求失败,状态代码404” 下面,我添加了React原生创建/删除函数和后端创建/删除函数的代码片段。还提供了React本机控制台屏幕截图和应用程序界面屏幕截图 反应本机创建和删除功能:Javascript React本机Axios请求失败,状态代码404,javascript,codeigniter,react-native,axios,Javascript,Codeigniter,React Native,Axios,我正在尝试使用AXIOS Post方法从数据库中删除数据 我在后端代码上创建了一个API delete函数,并使用POSTMAN进行验证。使用POSTMAN,我可以从数据库中创建和删除数据。然而,在React Native上,当调用delete函数时,我得到“请求失败,状态代码404” 下面,我添加了React原生创建/删除函数和后端创建/删除函数的代码片段。还提供了React本机控制台屏幕截图和应用程序界面屏幕截图 反应本机创建和删除功能: //Add date and time addDat
//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);
}
}
后端创建和删除功能:
//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);
}
}
React Native Console.log():
//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);
}
}
应用程序界面:
//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);
}
}
您是否可以添加一个在Postmanh中成功运行的屏幕截图?我已经解决了这个问题。这是我的RN Api的问题。在我的axios中,我发送的数据不正确。