角度6:HttpErrorResponse语法错误:JSON中的意外令牌s [英] Angular 6: HttpErrorResponse SyntaxError: Unexpected token s in JSON
问题描述
我正在发布请求,并且我想收到一个成功"字符串作为响应.我收到一个HttpResponseError,下面的图像中发布了以下信息.
I am posting a request and I am suppose to receive a 'success' string back as response. I am getting an HttpResponseError with the following information posted in the image below.
PurchaseOrderService
PurchaseOrderService
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'))
);
}
PurchaseOrderComponent
PurchaseOrderComponent
this.purchaseOrderService.postPurchaseOrderCustom(purchaseOrderCustom).subscribe( response => {
console.log("Testing", response);
},
error => {
console.error('errorMsg',error );
}
我的操作方式与文档中的操作方式相同.一定要向我指出我在做错什么.
The way I am doing it is the same way its done in the documentation. Do point out to me what I am doing wrong.
推荐答案
这与您的api响应类型有关,success
是无效的json格式,默认情况下,HttpClient
是预期的json响应类型,请稍后尝试解析.您可以通过将响应类型设置为 text 这样
This related to your api respond type ,success
is not valid json format,by default HttpClient
is expected json response type and try to parse it later . You can solve this by set the respond type to text like this
return this.http.post<String>(url, purchaseOrderCustom,
{headers: this.header , responseType:'text'})
.pipe (catchError(this.handleError('postPurchaseOrderCustom', 'I am an error')));
这篇关于角度6:HttpErrorResponse语法错误:JSON中的意外令牌s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!