简体   繁体   English

如何正确调用删除方法?

[英]how can I call a delete method properly?

It's actually the first time that I'm trying to call a delete method angular.这实际上是我第一次尝试调用 angular 的删除方法。 my code in my dataService is:我在 dataService 中的代码是:

deleteMeaningItem(data): Observable<Result> {
    return this.http.delete<Result>(url, data);
  }

and in component:并在组件中:

 this.dataService.deleteMeaningItem({id: id}).subscribe(res => {
 if (res.status) {
    //do something          
  }
  });

but I'm getting 415 Unsupported Media Type error!但我收到 415 Unsupported Media Type 错误! I've also tried to send Content-Type in my request header like:我还尝试在我的请求标头中发送 Content-Type ,例如:

deleteMeaningItem(data): Observable<Result> {
     return this.http.delete<Result>(global.dataUrl + '/MeaningItems/Delete', { params: data,
     headers: {'Content-Type': 'application/json'}});
}

but then then I'm getting 400 Bad Request error!但是后来我收到了 400 Bad Request 错误! I need your help.我需要你的帮助。

I guess the problem is might be in the data object.我想问题可能出在data对象中。 I will first check the docs of the API because it looks like you are missing also Autorization header that is usually needed for methods like delete我将首先检查 API 的文档,因为您似乎还缺少delete方法通常需要的Autorization标头

all I had to do was to provide body in the request option.我所要做的就是在请求选项中提供正文。 so I did this:所以我这样做了:

deleteMeaningItem(meaningId): Observable<Result> {
    const options = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
      }),
      body: meaningId,
    };
    return this.http.delete<Result>(global.dataUrl + '/MeaningItems/Delete', options);
  }

Maybe it's easier using RXJS.也许使用 RXJS 更容易。

First define the delete function @ Service首先定义删除函数@Service

delete(id: string): Observable<Result> {
 return this.httpClient.delete<Result>(`${global.dataUrl}/MeaningItems/${id}`);
}

Then try to call it like this:然后尝试像这样调用它:

this.service.delete(id).pipe(
  tap(r => console.log('success', r)),
  catchError(e => {
    console.error('error', e);
    return throwError(e);
  })
).subscribe();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Angular 中的删除方法在正文中传递数据? - How can I use the delete method in Angular to pass data in body? 如何在Angular 2中的promise中调用类方法? - How can I call a class method inside a promise in Angular 2? 如何使用@HostListener(&#39;window:beforeunload&#39;)调用方法? - How can I use @HostListener('window:beforeunload') to call a method? 一旦隐藏了特定的 BsModalRef 实例,如何调用方法? - How can I call a method once a specific BsModalRef instance is hidden? 测试角度组件:如何在 Jasmine 中使用 expect(method).toHaveBeenCalledWith(p) 测试异步方法调用之后的方法调用 - Testing angular components: how can I test method call that comes after an async method call with expect(method).toHaveBeenCalledWith(p) in Jasmine 如何调用方法I Sweetalert 2 - How to call method i sweetalert 2 删除方法中的 HTTP 错误无法正常工作 - HTTP error in delete method not working properly ionic ionchange 和 ngOnit 方法相同,当我两次进入页面时,方法会调用 3 次,我该如何避免呢? - ionic ionchange and ngOnit same method when i enter page twice ,method will call three times, how can i avoid it? 如何调用 Angular 中任何组件的特定公共方法,例如 `ngOnInit` 的工作原理 - How can I call a specific public method of any component in Angular, like how `ngOnInit` works 如何在不使用 hubconnection.On 方法显式调用它们的情况下调用所有信号 R 事件 - How can I call all the signal R events without explicitly invoking them using hubconnection.On method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM