如何从角度为9的http客户端删除方法获取文本响应

问题描述

我的代码中出现错误。我想要的是服务器的文本响应,但是当我编写下面的代码时,它会抛出运行时错误

代码

 public deleteChapterTopic(deleteUserId,clientId: number,grade: string,subjectId: number,chapterTopicId: number,parentChapterTopicId: number,chapterOrTopic: number) {
    let options = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',}),body: {
        deleteUserId: deleteUserId,chapterId: chapterTopicId,subjectId: subjectId,grade: grade,parentChapterTopicId: parentChapterTopicId,clientId: clientId,chapterOrTopic: chapterOrTopic
      },observe: "body",responseType: 'arraybuffer',};
 
    return this.http.delete(BackendService.CHAPTER_TOPIC,options);
  }

错误

   ERROR in src/app/backend/backend.service.ts:581:59 - error TS2769: No overload matches this call.
      The last overload gave the following error.
        Argument of type '{ headers: HttpHeaders; body: { deleteUserId: number; chapterId: number; subjectId: number; grade: string; parentChapterTopicId: number; clientId: number; chapterOrTopic: number; }; observe: string; responseType: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { 
[header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
          Types of property 'observe' are incompatible.
            Type 'string' is not assignable to type '"body"'.

    581     return this.http.delete(BackendService.CHAPTER_TOPIC,options);

我该如何解决此问题,以便我可以在有效载荷中发送数据并在文本中获得响应。 提前致谢。 ~~~~~~~

解决方法

什么是“身体”?应该是参数。

public deleteChapterTopic(deleteUserId,clientId: number,grade: string,subjectId: number,chapterTopicId: number,parentChapterTopicId: number,chapterOrTopic: number) {
    let options = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',}),params: {
        deleteUserId: deleteUserId,chapterId: chapterTopicId,subjectId: subjectId,grade: grade,parentChapterTopicId: parentChapterTopicId,clientId: clientId,chapterOrTopic: chapterOrTopic
      },observe: "body",responseType: 'arraybuffer',};
 
    return this.http.delete(BackendService.CHAPTER_TOPIC,options);
  }

,

您的代码表明您要向HTTP DELETE请求中添加主体。 HTTP 1.1规范(RFC 7231)明确允许DELETE请求中包含实体主体。

DELETE请求消息中的有效负载没有定义的语义;在DELETE请求上发送有效内容正文可能会导致某些现有实现拒绝该请求。

因此,使用Angular HTTTP客户端无法将主体添加到DELETE请求中。

但是,您可以在请求中包含由params属性定义的查询参数。就您而言,只需将body中的options重命名为params

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...