gpt4 book ai didi

json - 语法错误: Unexpected end of input in Angular 5 service

转载 作者:行者123 更新时间:2023-12-03 08:08:03 25 4
gpt4 key购买 nike

我知道这是一个普遍的问题,但是我仍然不明白为什么在我的案例中会复制它。几天后找不到答案。

遵循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。

最佳答案

从 Angular 最新更新:

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

因此,请尝试从.then(response => response.json() as T)删除.json()。

关于json - 语法错误: Unexpected end of input in Angular 5 service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47325747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com