angular - Angular 6: HttpErrorResponse SyntaxError: Unexpected token s in JSON
问题描述
我正在发布一个请求,并且我想收到一个“成功”字符串作为响应。我收到一个 HttpResponseError 并在下图中发布了以下信息。
采购订单服务
postPurchaseOrderCustom(purchaseOrderCustom: PurchaseOrderSaveCustom) {
const url = `${this.localUrl}purchaseOrderCustom`;
return this.http.post<String>(url, purchaseOrderCustom, {headers: this.header})
.pipe(
catchError(this.handleError('postPurchaseOrderCustom', 'I am an error'))
);
}
采购订单组件
this.purchaseOrderService.postPurchaseOrderCustom(purchaseOrderCustom).subscribe( response => {
console.log("Testing", response);
},
error => {
console.error('errorMsg',error );
}
我这样做的方式与文档中的方式相同。请向我指出我做错了什么。
解决方案
这与您的 api 响应类型有关,success
不是有效的 json 格式,默认情况下HttpClient
应为 json 响应类型并尝试稍后解析。您可以通过将响应类型设置为这样的文本来解决此问题
return this.http.post<String>(
url,
purchaseOrderCustom,
{ headers: this.header, responseType: 'text' }
)
.pipe(
catchError(this.handleError('postPurchaseOrderCustom', 'I am an error')
));
推荐阅读
- php - PHP | 来自 SQL 数据库的饼图
- java - 如何创建安全的 vaadin 14 登录表单?
- c - linux中两个虚拟内存如何指向同一个物理内存
- python - 如何在深度学习模型中训练不同尺度的特征
- algorithm - 谷歌量子霸权——一万年是如何假设的?
- jquery - 有没有办法在每个 iframe 中获取父 css 链接 rel?
- git - Git状态查看仅包含空格的文件会更改吗?
- spring-boot - 如何从 web.xml 在 weblogic.xml 中引入路径变量?
- c++ - Boost.Beast 限速
- office-js - 在 web 界面中添加位置后调用 enhancedLocation.getAsync() 时,返回一个空列表