我在laravel中创建了一个API控制器。它取得了成功。数据可以删除。现在,我尝试在ionic中创建删除代码。当用户单击“删除”图标时,可以从离子数据以及数据库中删除数据。
我将离子代码中的代码更改为GET函数,但是它无法正常工作,实际上我还找不到解决方案。
这是我在laravel中所做的,在邮递员中测试api时可以删除哪些数据。
api.php
public function deleteMed(Request $request)
{
$id = $request->id;
$medname = $request->medname;
$price = $request->price;
$stock = $request->stock;
$medno = $request->medno;
$ingredient = $request->ingredient;
$description = $request->description;
$addinfo = $request->addinfo;
$user = AddMedicine::where('medno', $request->input('medno'))->count();
if($user!=0)
{
AddMedicine::where('id', $id)->delete([
'id' =>$id,
'medname'=>$medname,
'price'=>$price,
'stock'=>$stock,
'medno'=>$medno,
'ingredient'=>$ingredient,
'description'=>$description,
'addinfo'=>$addinfo,
]);
$msg = "Data deleted";
$datamsg = response()->json([
'success' => $msg
]);
return $datamsg->content();
}
else{
echo'{"error":{"text":" Medicine does not exist"}}';
}
}
这是我在ionic中完成的代码。
viewMed.ts
//alert delete data
confirmdelete() {
let alert = this.alertCtrl.create({
title: 'Confirm Delete',
message: 'Do you confirm to delete?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('Cancel clicked');
}
},
{
text: 'Yes',
handler: () => {
console.log('Yes clicked');
this.deleteData(); // call deleteData()
}
}
]
});
alert.present();
}
deleteData(){
this.authService.deleteData(this.userData,'deleteMed').then((result) => {
this.responsedata = result;
if(this.responsedata.success){
console.log(this.responsedata.success);
localStorage.removeItem("userData");
localStorage.setItem('userData', JSON.stringify({"id": this.responsedata.success}));
//navparam
this.navCtrl.push("ViewMedicinePage",({"id": this.responsedata.success}));
}
else
{
this.presentError(this.responsedata.error.text);
console.log(this.responsedata.error);
}
}, (err) => {
});
}
presentError(msg) {
let toast = this.toastCtrl.create({
message: msg,
duration: 3000,
position: 'bottom',
cssClass: "withdraw_error"
});
toast.present(toast);
}
我还在文件夹提供者的authservice.ts中创建了删除功能。
authService.ts
deleteData(credentials, type){
return new Promise((resolve, reject) => {
let headers = new Headers();
this.http.delete(apiUrl+type, JSON.stringify(credentials),
{headers:headers}).subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
});
}
但是在此代码下,authservice.ts文件出现错误 该错误预期为1-2个参数,但得到3个
我希望可以在用户单击删除按钮并确认删除时删除数据。然后,在laragon数据库中更新数据删除。
答案 0 :(得分:0)
删除请求不接受正文参数,因此,如果要传递正文数据,请尝试
let headers = new Headers();
const options = {
headers: headers,
body: credentials
}
return new Promise((resolve, reject) => {
this.http.delete(apiUrl+type, options).subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
});