Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python JSON数据未通过Django REST API_Python_Json_Django_Angular - Fatal编程技术网

Python JSON数据未通过Django REST API

Python JSON数据未通过Django REST API,python,json,django,angular,Python,Json,Django,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 { constr

我有一个带有角度前端的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

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

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

angular应用程序和django运行在不同的端口吗?啊,是的!我想那很糟糕吧?还是很好?一个在端口8000上运行,另一个在端口4200上运行控制台中有错误吗?没有错误,但会发布控制台反馈django服务器的终端反馈说:[30/Nov/2017 18:36:37]“GET/weatherstats/weatherdata/HTTP/1.1”200 6365027所以我假设它正在接收请求并提供数据,如果这是新的东西,我可以建议阅读以下内容:一个非常常见的陷阱是尝试在订阅之外访问数据,所以提醒一下:)