[英]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.