作者:弱柳纤纤_ | 来源:互联网 | 2022-12-09 12:02
我发布了一个请求,我想收到一个'成功'字符串作为响应.我收到一个HttpResponseError,下面的图片中显示了以下信息.
PurchaseOrderService
postPurchaseOrderCustom(purchaseOrderCustom: PurchaseOrderSaveCustom) {
const url = `${this.localUrl}purchaseOrderCustom`;
return this.http.post(url, purchaseOrderCustom, {headers: this.header})
.pipe(
catchError(this.handleError('postPurchaseOrderCustom', 'I am an error'))
);
}
PurchaseOrderComponent
this.purchaseOrderService.postPurchaseOrderCustom(purchaseOrderCustom).subscribe( respOnse=> {
console.log("Testing", response);
},
error => {
console.error('errorMsg',error );
}
我这样做的方式与文档中的完成方式相同.请指出我做错了什么.
1> malbarmawi..:
这与您的api响应类型有关,success
是无效的json格式,默认情况下HttpClient
是预期的json响应类型,并尝试稍后解析它.您可以通过将响应类型设置为这样的文本来解决此问题
return this.http.post(url, purchaseOrderCustom,
{headers: this.header , responseType:'text'})
.pipe (catchError(this.handleError('postPurchaseOrderCustom', 'I am an error')));
这就是我所做的`{headers:this.header,responseType:'text'as'json'})`.只是添加文字对我没用.从Github的讨论中,人们补充说要让它发挥作用