Angular 8错误:http解析期间Http失败 [英] Angular 8 error: Http failure during parsing for http

查看:58
本文介绍了Angular 8错误:http解析期间Http失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从Angular 8应用程序调用服务时收到错误.这是服务:

I get an error when trying to call a service from Angular 8 app. Here is the service:

const httpOptionsPlain = {
  headers: new HttpHeaders({ 'Accept': 'text/plain',
                             'Content-Type': 'text/plain'                             
                             })
};

@Injectable()
export class TripService {

public getTripNameById(tripId: number): Observable<any> {
    return this.http.get<any>(`${this.baseUrl}/Trips/Trip/Name/${tripId}`,  httpOptionsPlain);
  }

这是Java rest api(从浏览器调用时可以正常工作):

And here is the Java rest api (works fine when calling from the browser):

@GET
@Path("Trip/Name/{fundId}")
@Produces("text/plain")     
public String getTripNameById(@PathParam("tripId") Integer tripId) {        
    return myDao.getNameById(tripId);       
}

我在chrome控制台中遇到错误:

I'm getting the error in the chrome console:

错误:{错误:语法错误:JSONH中XML 0tt处JSON.parse()位置0处的意外令牌A,文本:"AAA BBB CCC"}标头:HttpHeaders {normalizedNames:Map(0),lazyUpdate:空,lazyInit:ƒ}消息:在解析 http://localhost:8080/ ...名称:"HttpErrorResponse"

error: {error: SyntaxError: Unexpected token A in JSON at position 0 at JSON.parse () at XMLHtt…, text: "AAA BBB CCC"} headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ} message: "Http failure during parsing for http://localhost:8080/... name: "HttpErrorResponse"

我发送的是纯文本,所以我不是为什么该服务尝试解析json.

I'm sending plain text so I'm not why the service try to parse json.

推荐答案

请尝试

const httpOptionsPlain = {
  headers: new HttpHeaders({
    'Accept': 'text/plain',
    'Content-Type': 'text/plain'
  }),
  'responseType': 'text'
};

@Injectable()
export class TripService {

public getTripNameById(tripId: number): Observable<string> {
    return this.http.get<string>(`${this.baseUrl}/Trips/Trip/Name/${tripId}`,  httpOptionsPlain);
  }

我刚刚将'添加到responseType

I've just added ' to the responseType

这篇关于Angular 8错误:http解析期间Http失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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