Angular 6: HttpErrorResponse SyntaxError: Unexpected token s in JSON [英] Angular 6: HttpErrorResponse SyntaxError: Unexpected token s in JSON

查看:31
本文介绍了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')
        ));

这篇关于Angular 6: HttpErrorResponse SyntaxError: Unexpected token s in JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆