gpt4 book ai didi

json - Angular 6 应用程序中的错误 SyntaxError : Unexpected token < in JSON at position 0 at JSON. 解析()

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

我在谷歌上搜索过它,是的,我明白我收到的是context/type:text/html而不是application/json,但我不明白如何来解决这个问题。当我尝试从本地服务器访问第三方用户的 api 时,出现此错误。请检查此错误的屏幕截图。

enter image description here

enter image description here

服务.ts

    export class VamosysService {
constructor(private _http:Http){}

getVechicalLocations():Observable<any[]>{
return this._http.get(API_URL)
.pipe(map((response:Response)=<any[]>response.json()));

}

组件.ts

export class VamosysComponent implements OnInit {

vechicalLocation:any[];

constructor(private _vamoService:VamosysService) { }
ngOnInit() {
this._vamoService.getVechicalLocations()
.subscribe((data) => this.vechicalLocation = data);
}



}

提前致谢

最佳答案

您正在使用HttpModule,该模块已已弃用,您应该使用 HttpClientModule 相反
在新的 HttpClientModule 中,JSON 是假定的默认值,不再需要使用 res.json()
进行显式解析服务

 import { HttpClient } from '@angular/common/http';
export class VamosysService {
constructor(private _httpc:HttpClient){}

getVechicalLocations():Observable<any[]>{
return this._httpc.get(API_URL);}

组件

 export class VamosysComponent implements OnInit {

public vechicalLocation=[];

constructor(private _vamoService:VamosysService) { }
ngOnInit() {
this._vamoService.getVechicalLocations()
.subscribe((data) => this.vechicalLocation=data);
}}

如果您请求非 JSON 数据您可以使用 responseType 属性。

getVechicalLocations():Observable<any[]>{
return this._httpc.get(API_URL,{ responseType: 'text' });}

响应类型可以是 responseType?: 'arraybuffer' | ' Blob ' | 'json'| '文本'

关于json - Angular 6 应用程序中的错误 SyntaxError : Unexpected token < in JSON at position 0 at JSON. 解析(<anonymous>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50540413/

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