来自Django的JSON数据不是通过Django API的

2024-05-19 22:10:54 发布

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

我有一个Django restapi和一个有角度的前端。我在前端没有得到错误,但是没有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)
    })
  }
}

喂入这样的组件:

^{pr2}$

我对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

Tags: 数据importjsonfalsehttpsubscriptionsthisarray
1条回答
网友
1楼 · 发布于 2024-05-19 22:10:54

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

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

相关问题 更多 >