【问题标题】:Angular front end not making rest call to Flask ApplicationAngular 前端没有对 Flask 应用程序进行休息调用
【发布时间】:2019-01-28 15:26:31
【问题描述】:

我有一个烧瓶应用程序在这个 url http://127.0.0.1:5000/SectionA 中运行 我正在使用 return jsonify(data) 返回 json 格式的数据。当我从浏览器中点击 url 时,它看起来像这样

我正在像这样从 Angular 应用程序 main-service.ts 文件中调用这个 url

 public getVamData(id,section): Observable<any> {
         try {
         console.log("I am here getVAM");
         return this.http.get('http://localhost:5000/SectionA')
                .map((response: Response) => {
                    console.log("I am here getVAM2");
                    return response.json();
                }
            );
           }
        catch(e) {
        console.log('Error:', e);
        }   }

但 url 没有命中烧瓶应用程序。请让我知道我缺少什么。

【问题讨论】:

  • ...你在哪里订阅?

标签: javascript angular rest


【解决方案1】:

你必须订阅 Observable 因为 Observable 是延迟加载的。 如果您在其他地方(如组件)使用 getVamData() 服务方法,请在那里订阅。 像这样使用

this.mainService.getVamData(this.paramData.Eid,'SectionA')
.subscribe(result=>{
  this.data=result;
console.log(this.data);
})

【讨论】:

  • 我在 vam_component.ts 文件构造函数(public route:ActivatedRoute,public mainService:MainServiceService) { this.route.params.subscribe(params => { this.paramData=params; console. log(this.paramData) this.data=this.mainService.getVamData(this.paramData.Eid,'SectionA'); console.log(this.data);
  • 我进行了更改,但仍然无法访问它。我有 2 个应用程序 django 在端口 8080 中运行,flask 在 5000 中运行。我能够调用 django rest api url 但无法调用 flask api url。我应该对配置文件进行一些更改以访问在不同端口上运行的两个应用程序吗?
  • 我已将烧瓶应用程序部分移入 Django 休息框架工作,现在我在 main-service.ts 中得到响应,但我在this.mainService.getVamData(this.paramData.Eid,'SectionA').subscribe(result =&gt;{ this.data=result; console.log(this.data); }, error =&gt; console.log(error));中得到未定义的数据
  • 现在我得到了响应,但订阅递归运行。我想在一轮迭代后将我的 json 响应存储在一个变量中,并处理该 json 数据,例如 var temp=this.http.get('http://localhost:8000/vamcomponent/') 请解释我如何做到这一点。
  • 让 classes=this.data[1].classes;因为您的响应数据是数组,而 classes 是索引 1 处的字段名称。现在使用 classes 变量进行循环,让我它为您工作不是
猜你喜欢
  • 2020-06-16
  • 2019-03-12
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 2017-05-27
  • 1970-01-01
  • 2022-12-18
  • 2018-11-25
相关资源
最近更新 更多