typescript SyntaxError:Angular 5 服务中的输入意外结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47325747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: Unexpected end of input in Angular 5 service
提问by Seva
I know that it is a common problem, but I still don't understand why it is reproduced in my case. Сouldn't find an answer in a few days.
我知道这是一个常见问题,但我仍然不明白为什么在我的情况下会重现它。几天之内找不到答案。
Folowing a part of the Angular service:
遵循 Angular 服务的一部分:
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Response, RequestOptionsArgs, ResponseContentType } from '@angular/http';
import { Http, RequestOptions, Request, RequestMethod, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';
@Injectable()
export class BaseService {
private headers: Headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8', 'Data-Type': 'json' });
constructor(private http: Http, private router: Router, ) {
}
public get<T>(url: string, options?: RequestOptionsArgs): Promise<T> {
return this.http.get(url, options)
.toPromise()
.then(response => response.json() as T)
.catch(this.handleError.bind(this));
}
public handleError(error: any) {
if (error.status === 400) {
switch (error.statusText) {
case 'Authentication':
this.router.navigate(['/error/auth']);
break;
default:
return Promise.reject(error);
}
return;
}
return Promise.reject(error.toString());
}
}
Folowing method called base method:
以下方法称为基本方法:
getSearchResultTable(hash: number): Promise<Prices[]> {
const tmp = this.baseService.get<Prices[]>(this.apiUrl + hash);
return tmp;
}
Json that's returned from server is valid. I checked it one some online service.
从服务器返回的 Json 是有效的。我检查了它一些在线服务。
But I see SyntaxError: Unexpected end of input
error in Response
when trying to debug.
但是我SyntaxError: Unexpected end of input
在Response
尝试调试时看到错误。
Call Stack:
调用堆栈:
SyntaxError: Unexpected end of input
at BaseService.get (webpack-internal:///../../../../../src/app/search-module/services/base.service.ts:36:14)
at SearchResultService.getSearchResultTable (webpack-internal:///../../../../../src/app/search-module/services/search-result.service.ts:30:36)
at SearchResultPageComponent.ngOnInit (webpack-internal:///../../../../../src/app/search-module/pages/search-result/search-result.component.ts:24:34)
at checkAndUpdateDirectiveInline (webpack-internal:///../../../core/esm5/core.js:12291:19)
at checkAndUpdateNodeInline (webpack-internal:///../../../core/esm5/core.js:13794:20)
at checkAndUpdateNode (webpack-internal:///../../../core/esm5/core.js:13737:16)
at debugCheckAndUpdateNode (webpack-internal:///../../../core/esm5/core.js:14609:76)
at debugCheckDirectivesFn (webpack-internal:///../../../core/esm5/core.js:14550:13)
at Object.eval [as updateDirectives] (ng:///SearchModule/SearchResultPageComponent_Host.ngfactory.js:9:5)
at Object.debugUpdateDirectives [as updateDirectives] (webpack-internal:///../../../core/esm5/core.js:14535:21)
I'm using latest version of Angular.
我正在使用最新版本的 Angular。
回答by Nimish goel
From angular latest update:
从角度最新更新:
remove any map(res => res.json()) calls, which are no longer needed.
删除任何不再需要的 map(res => res.json()) 调用。
So try to remove .json() from .then(response => response.json() as T)
.
所以尝试从 .json() 中删除.then(response => response.json() as T)
.