http.delete works fine on Postman but not on angular 4/ionic 3 app

时间:2017-10-12 09:39:08

标签: angular http ionic3

i m a 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

1 个答案:

答案 0 :(得分:0)

我解决了,它是搜索的参数,我改变了代码

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)

     }

到:

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)

     }

“事件”是返回[object Object]的东西,我确实放了一个JSON.stringify来验证它的内容,它保存了整个请求,我只想把rfcnumber作为搜索删除的参数。< / p>

希望它可以帮助任何有类似问题的人