1

我知道这是一个常见问题,但我仍然不明白为什么在我的情况下会重现它。几天后找不到答案。

遵循 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());
    }
}

以下方法称为基本方法:

getSearchResultTable(hash: number): Promise<Prices[]> {
    const tmp = this.baseService.get<Prices[]>(this.apiUrl + hash);
     return tmp;
}

从服务器返回的 Json 是有效的。我检查了一些在线服务。

但是我在尝试调试时看到SyntaxError: Unexpected end of input错误。Response

调用堆栈:

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)

我正在使用最新版本的 Angular。

4

1 回答 1

1

从角度最新更新:

删除不再需要的任何 map(res => res.json()) 调用。

所以尝试从 .json() 中删除.then(response => response.json() as T)

于 2017-11-16T10:42:25.800 回答