JSON数据不来自Django REST API

达夫索1983

我有一个带有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
AJT82

现在,您正在控制台中记录预订,因此您在控制台中获得的输出是正确的。要控制台记录响应,请执行以下操作:

this.weatherstatsservice.getWeatherData()
  .subscribe(data => {
    this.data = data;
    console.log(this.data)
  })

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章