我使用Flask在Python中编写了一个简单的API。我还使用stackblitz.com在Angular中编写了一个简单的应用程序。当我尝试从服务器获取数据时,服务器返回HTTP 200响应,但控制台为loggin HttpErrorResponse,其中包含以下信息: 错误:ProgressEvent 标头:HttpHeaders 消息:“ http://127.0.0.1:5000/test的Http故障响应:0未知错误” 名称:“ HttpErrorResponse” 好的:错误 状态:0 statusText:“未知错误” 网址:“ http://127.0.0.1:5000/test” 原始:HttpErrorResponse
我已经在Postman中测试了API,并且可以正常工作。
API
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/test')
def test():
return jsonify(
{
"protoid": 1,
"operator": "Example",
}
)
app.run()
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { GetterService } from './getter.service';
import { TestComponent } from './test/test.component';
@NgModule({
imports: [ BrowserModule, FormsModule, HttpClientModule ],
declarations: [ AppComponent, HelloComponent, TestComponent ],
bootstrap: [ AppComponent ],
providers: [GetterService]
})
export class AppModule { }
getter.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs'
@Injectable()
export class GetterService {
constructor(private http: HttpClient) { }
_url = 'http://127.0.0.1:5000/test'
getObject(): Observable <Object> {
return this.http.get(this._url)
}
}
test.component.ts
import { Component, OnInit } from '@angular/core';
import { GetterService } from '../getter.service';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
constructor(private getter: GetterService) { }
ngOnInit() {
}
object: Object;
getObject() {
return this.getter.getObject()
.subscribe(data => this.object = data)
}
}
我想将JSON对象保存到对象变量中,但是我正在获取HttpErrorResponse