角度6:HttpErrorResponse语法错误:JSON中的意外令牌s [英] Angular 6: HttpErrorResponse SyntaxError: Unexpected token s in JSON

查看:266
本文介绍了角度6:HttpErrorResponse语法错误:JSON中的意外令牌s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发布请求,并且我想收到一个成功"字符串作为响应.我收到一个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屋!

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