问题描述
我的代码中出现错误。我想要的是服务器的文本响应,但是当我编写下面的代码时,它会抛出运行时错误。
代码:
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
。