简体   繁体   English

Angular2异步/在位置0的共享服务JSON中出现错误

[英]Angular2 async/promises in shared-service JSON error at position 0

I'm getting an error Unexpected token < in JSON at position 0 in my shared-service to call an API. 我在共享服务Unexpected token < in JSON at position 0收到错误的Unexpected token < in JSON at position 0以调用API。 I think its because of async request as sometimes the data is available and sometimes this error shows. 我认为这是由于异步请求造成的,因为有时数据可用,有时还会显示此错误。

The error log is point to this request. 错误日志指向此请求。

public getStores(): void {
 this.retrieveResults().subscribe((results) => {
  this.storesChanged.next(results.Results)
 });
}

public retrieveResults(): Observable<any> {
 return this.http.get(__env.nodeRedApiUrl + __env.storeSummaryPath)
  .map(res => res.json())
}

can someone point out why this error is actually showing? 有人可以指出为什么实际上显示此错误吗? I just thought i'd be async call. 我只是认为我会被异步调用。 Can someone direct me how to deal with async/promise in Angular2. 有人可以指导我如何处理Angular2中的异步/承诺。

This happens mostly when you return HTML instead of json as expected. 大多数情况是在您返回HTML而不是预期的json时发生的。 So check backend to make sure json is returned. 因此,请检查后端以确保json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM