简体   繁体   中英

http.delete works fine on Postman but not on Angular 4/Ionic 3 app

I ma beginner in Angular 2/Spring boot stack, so please bear with me a bit.

I have created an http.delete method for deleting a particular row selected by the user which prompts an Alert to verify the deletion, my request actually works fine on Postman as I could delete a selected row based on the params given completely from the database, but not in my frontend app, so my API works well as intended.i even checked for Subscribe since Observable are lazy, I did several combinations of code but here's the actual Angular code:

IncidentService.ts:

supprimerIncident(incident)
{
  let cHeaders = new Headers({ 'Content-Type': 'application/json' });
   let cParams = new URLSearchParams();
   cParams.set('rfcnumber',incident);
   let options = new RequestOptions({ headers: cHeaders, params: cParams })
   return this.http.delete(Api+'delete',options)

     }

And here's the code for the incident.ts file calling this service.

    deleter(incident:string)
  {
    let alert = this.alertCtrl.create({
        title: 'Confirmation',
        message: 'Voulez-vous supprimer ce ticket ?',
        buttons: [{
          text: "Ok",
          handler: () => { 

              this.incserv.supprimerIncident(incident).
             map(res=>res.status)
              .subscribe(res=>
  {
      for(let i = 0; i < this.incidents.length; i++) {

            if(this.incidents[i] == incident){
               this.incidents.splice(i, 1);

              }
  }
    //console.log(this.incidents=incident);

    console.log(res)
   },
    err=>{
  console.log(err);

  return Observable.throw(err.status);
  });
            }
        }, {
          text: "Annuler",
          role: 'cancel'
        }]
      })
      alert.present();


  }

The splice method works and deletes the intended row from the list, but when I run the app again, the row is back there, it's still in the database, can be deleted directly from Postman, but not the app. I appreciate your help and time given, thank you very much.

Edit: here's the screenshot for the request network (xhr request result)

在此处输入图片说明

I solved it, it was the params of the search , i changed the code from

supprimerIncident(incident)
{
  let cHeaders = new Headers({ 'Content-Type': 'application/json' });
   let cParams = new URLSearchParams();
   cParams.set('rfcnumber',incident);
   let options = new RequestOptions({ headers: cHeaders, params: cParams })
   return this.http.delete(Api+'delete',options)

     }

to :

supprimerIncident(incident)
{
  let cHeaders = new Headers({ 'Content-Type': 'application/json' });
   let cParams = new URLSearchParams();
   cParams.set('rfcnumber',incident.rfcnumber);
   let options = new RequestOptions({ headers: cHeaders, params: cParams })
   return this.http.delete(Api+'delete',options)

     }

"incident" was what returning the [object Object] , i did put a JSON.stringify to verify its content, it held the whole request,wherea i wanted just the rfcnumber as a param for the search for the deletion.

Hope it helps anyone having a similar issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM