使用Angular 8 HttpClient和Python Flas时出现未知错误

2024-06-25 23:45:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python Flask后端服务,它在API上使用GET调用时提供一个JSON,如下所示:

查询:

curl -XGET http://localhost:5000/bizLocations

回应:

^{pr2}$

我真的只想在一个下拉列表中显示通过Angular构建的前端列表

后端

代码段:

^{3}$

前端

我创建了一个服务:

component.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class ComponentService {

    private backEnd = 'http://localhost:5000';

    constructor(private http: HttpClient) { }

    getBizLocations() {
        return this.http.get(`${this.backEnd}/bizLocations`);
    }
}

在组件中,我按如下方式调用服务:

在组件.ts

ngOnInit() {
  this.getBusinessLocations();
}

getBusinessLocations() {
        this.componentService.getBizLocations()
            .subscribe((data: any) => {
                console.log(data);
                this.locations = data.locations;
            });
    }

错误

Error Log in Console

是什么导致了这个错误?是与CORS或{}有关吗?我没有他们两个,因为应用程序仍在测试和开发中。 相反,我可以通过Websockets与后端通信


Tags: fromimportbackendlocalhosthttp列表dataprivate