【发布时间】:2018-05-14 18:36:09
【问题描述】:
我有一个带有 Angular 前端的 Django REST API。我在前端没有收到任何错误,但没有 json 数据记录到控制台。
我有这样的服务:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class WeatherstatsService {
constructor(private http:Http) {
console.log("Hello World");
this.getWeatherData();
// this.getWeatherDataRecords();
}
weatherdata: any = {};
private _WeatherStatsUrl = 'http://127.0.0.1:8000/weatherstats/weatherdata';
getWeatherData() {
return this.http.get(this._WeatherStatsUrl)
.map((res:Response) => res.json());
}
getWeatherDataRecords() {
this.getWeatherData().subscribe(weatherdata => {
console.log(weatherdata)
})
}
}
像这样喂一个组件:
import { Component, OnInit } from '@angular/core';
import { WeatherstatsService } from '../weatherstats.service';
@Component({
selector: 'weather-stats',
templateUrl: './weather-stats.component.html',
styleUrls: ['./weather-stats.component.css']
})
export class WeatherStatsComponent implements OnInit {
data: any;
constructor(private weatherstatsservice: WeatherstatsService) { }
ngOnInit() {
console.log(this.weatherstatsservice.getWeatherData().subscribe(data => this.data = data));
}
}
我是 Django 和 Angular 的新手,当我在浏览器中测试 API 时,我得到了我想要的数据。这意味着我看不到我的 json 数据会发生什么? (向刚才试图帮助解决类似问题的人道歉 - 我的大脑正在为所有这些新东西爆炸!)
控制台:
Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null, …}
closed
:
false
destination
:
SafeSubscriber {closed: false, _parent: null, _parents: null, _subscriptions: null, syncErrorValue: null, …}
isStopped
:
false
syncErrorThrowable
:
false
syncErrorThrown
:
false
syncErrorValue
:
null
_parent
:
null
_parents
:
null
_subscriptions
:
Array(1)
0
:
MapSubscriber {closed: true, _parent: null, _parents: null, _subscriptions: null, syncErrorValue: null, …}
length
:
1
__proto__
:
Array(0)
__proto__
:
Subscription
【问题讨论】:
-
Angular 应用程序和 django 是否在不同的端口运行?
-
啊,是的!我想这很糟糕?还是很好?一个在 8000 端口上运行,另一个在 4200 端口上运行
-
控制台有错误吗?
-
没有错误,但会发布控制台提要
-
django 服务器的终端提要显示:[30/Nov/2017 18:36:37] "GET /weatherstats/weatherdata/ HTTP/1.1" 200 6365027 所以我假设它正在接收请求和提供数据
标签: python json django angular