【发布时间】:2019-11-13 18:55:54
【问题描述】:
尝试从 Angular 8 应用程序调用服务时出现错误。 这是服务:
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(从浏览器调用时可以正常工作):
@GET
@Path("Trip/Name/{fundId}")
@Produces("text/plain")
public String getTripNameById(@PathParam("tripId") Integer tripId) {
return myDao.getNameById(tripId);
}
我在 chrome 控制台中遇到错误:
error: {error: SyntaxError: Unexpected token A in JSON at JSON.parse () at XMLHtt…, text: "AAA BBB CCC"} 标头:HttpHeaders {normalizedNames:Map(0),lazyUpdate:null,lazyInit:ƒ} 消息:“http://localhost:8080/ 解析期间的 Http 失败... 名称:“HttpErrorResponse”
我发送的是纯文本,所以我不是服务尝试解析 json 的原因。
【问题讨论】: