本文介绍了使用HttpClient删除方法发送正文(角度5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Angular 5应用程序中的Http迁移到HttpClient,一切顺利,直到我使用一种服务的方法:

I'm migrating Http to HttpClient in an Angular 5 application and everything went fine until I got to this method of one of the services:

deleteMultipleObjects(userId: number, officeId : number, objectsData : any) {
  const url = `${APP_URL}/delete-objects/${userId}/${officeId}`;

  let headers = new Headers({ 'Content-Type': 'application/json' });

  this.appOptionsService.log("Deleting multiple objects.....", url, objectsData);

  return this.http
    .delete(url, { headers : headers, body : objectsData } )
    .map(this.extractData)
    .catch(this.handleError);
}

如您所见,我在delete调用的主体中传递了一个对象objectsData,该对象基本上具有以下结构:

As you can see, I pass an object objectsData in the body of the delete call that basically has this structure:

{
  objectIds: [id1, id2, id3, ...]
}

我看到HttpClient中的delete方法不允许包含主体,因此如何发送对象ID数组以通过调用删除?我有很多与此调用相同的删除调用,因此我必须在代码中进行很多更改以使其适应...

I see that the delete method in the HttpClient doesn't allow to include a body, so how could I send the array of object ids to delete with the call? I have a lot of delete calls that are the same as this one, so I'll have to change a lot of things in the code to adapt it...

谢谢!

推荐答案

您无法在Angular的DELETE http请求中发送正文.

You can not send body in DELETE http request in Angular.

HTTP规范说这是可能的. 文档页面.

HTTP specification on MDN says that this possible. documentation page.

如果需要此功能,则应使用PUT作为解决方法.允许多个的另一种方法ID在您的删除API端点上.例如:

If you need this functionality, you should use PUT as workaround. Another method to allow multiple e.g. id's on your delete API endpoint. For example:

https://server.com/api/delete?ids=1,2,3,4,5

这需要对您的api进行一些更改.

That requires some changes on your api.

这篇关于使用HttpClient删除方法发送正文(角度5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 15:09