Angular 4 http 未返回响应
Angular 4 http not returning response
我创建了一个简单的服务来通过 http 请求获取数据。数据采用 json 形式,我使用 map 函数使其与 Blog 数据类型相匹配。但是,当我从服务调用方法 "getBlogs" 以获取数据时,没有任何返回。对我缺少的东西有什么想法吗?
组件。
import {Http} from '@angular/http';
import {forEach} from '@angular/router/src/utils/collection';
import {BlogService} from '../services/blog.service';
import {Component, OnInit} from '@angular/core';
import {Blog} from '../blog/blog';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';
@Component({selector: 'app-blog-list', templateUrl: './blog-list.component.html',
styleUrls: ['./blog-list.component.css'],
providers: [BlogService]})
export class BlogListComponent implements OnInit {
blogArray: Blog[] = [];
blogTitle: string;
loading: boolean;
private blogsUrl = 'http://www.mocky.io/v2/59a49eb3100000be05b2aba6';
constructor(private blogService: BlogService, private http: Http) {}
ngOnInit() {
this.blogService
.getBlogs(this.blogsUrl)
.subscribe(res => {
this.blogArray = res;
console.log('theBlogArray', this.blogArray);
});
}
}
服务
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';
import {Headers, Http } from '@angular/http';
import { Injectable } from '@angular/core';
import {Blog} from '../blog/blog';
@Injectable()
export class BlogService {
private loading: boolean;
constructor(private http: Http) {}
getBlogs(url: string): Observable<Blog[]> {
this.loading = true;
return this.http.get(url)
.map((response) => response.json());
}
}
如答案中所述,这可能是 CORS 问题。但是,我在控制台中根本没有收到任何错误。以防万一我在我的浏览器上启用了 CORS chrome 扩展(它被称为 Allow-Control-Allow-Origin: *),当我遇到 CORS 问题时我通常会这样做,并且我确保将它放在 header 的嘲笑,再次按照建议。还没有,控制台没有抱怨,webpack 编译成功。
如果我将 json 请求的值硬编码到服务中的一个数组中并通过组件访问它,这些值将返回,我可以 console.log 它们并将它们显示在 html.
订阅外的console print
不会等待response.So试赞
ngOnInit() {
this.blogService.getBlogs(this.blogsUrl)
.subscribe(res => {
this.blogArray = res;
console.log('theBlogArray: ', this.blogArray);
}, Err => console.log(Err));
}
.
// Service
import 'rxjs/add/operator/catch';
return this.http.get(url)
.map((response) => response.json())
.catch(Err);
ngOnInit() {
this.blogService.getBlogs(this.blogsUrl)
.subscribe(res => {this.blogArray = res;
console.log('theBlogArray: ', this.blogArray);// this will work and use it in template like{{blogArray}}
);
}
您正在获取数据,唯一的事情是您正在控制台将其记录到外部,因为它是异步调用,您无法在同步上下文中登录。
更新
我使用 angular 尝试了同样的 url 它给出了一些 CORS 问题后端有问题请检查控制台。你会得到错误
我没有看到来自 http 请求的响应的原因是因为我在主应用程序组件中引用了 blog-list 组件,而没有在主应用程序组件中导入 blog-list。
我在做这个
<app-nav-bar></app-nav-bar>
<app-blog-list></app-blog-list>
这需要我这样做
import { Component } from '@angular/core';
import { BlogListComponent } from './blog-list/blog-list.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
}
我仍然不确定为什么我能够从服务而不是从 http 请求访问硬编码数组的值,而无需将 blog-list.component 导入主应用程序。
blog.service
public blogExample: Blog;
private loading: boolean;
constructor(private http: Http) {
this.blogExample = {
id: 1,
title: 'title',
content: 'content'
};
}
博客-list.component
ngOnInit() {
this.blogService
.getBlogs(this.blogsUrl)
.subscribe(res => {
this.blogArray = res;
console.log('theBlogArray', this.blogArray);
});
console.log('blogExample', this.blogService.blogExample);
}
我创建了一个简单的服务来通过 http 请求获取数据。数据采用 json 形式,我使用 map 函数使其与 Blog 数据类型相匹配。但是,当我从服务调用方法 "getBlogs" 以获取数据时,没有任何返回。对我缺少的东西有什么想法吗?
组件。
import {Http} from '@angular/http';
import {forEach} from '@angular/router/src/utils/collection';
import {BlogService} from '../services/blog.service';
import {Component, OnInit} from '@angular/core';
import {Blog} from '../blog/blog';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';
@Component({selector: 'app-blog-list', templateUrl: './blog-list.component.html',
styleUrls: ['./blog-list.component.css'],
providers: [BlogService]})
export class BlogListComponent implements OnInit {
blogArray: Blog[] = [];
blogTitle: string;
loading: boolean;
private blogsUrl = 'http://www.mocky.io/v2/59a49eb3100000be05b2aba6';
constructor(private blogService: BlogService, private http: Http) {}
ngOnInit() {
this.blogService
.getBlogs(this.blogsUrl)
.subscribe(res => {
this.blogArray = res;
console.log('theBlogArray', this.blogArray);
});
}
}
服务
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';
import {Headers, Http } from '@angular/http';
import { Injectable } from '@angular/core';
import {Blog} from '../blog/blog';
@Injectable()
export class BlogService {
private loading: boolean;
constructor(private http: Http) {}
getBlogs(url: string): Observable<Blog[]> {
this.loading = true;
return this.http.get(url)
.map((response) => response.json());
}
}
如答案中所述,这可能是 CORS 问题。但是,我在控制台中根本没有收到任何错误。以防万一我在我的浏览器上启用了 CORS chrome 扩展(它被称为 Allow-Control-Allow-Origin: *),当我遇到 CORS 问题时我通常会这样做,并且我确保将它放在 header 的嘲笑,再次按照建议。还没有,控制台没有抱怨,webpack 编译成功。
如果我将 json 请求的值硬编码到服务中的一个数组中并通过组件访问它,这些值将返回,我可以 console.log 它们并将它们显示在 html.
订阅外的console print
不会等待response.So试赞
ngOnInit() {
this.blogService.getBlogs(this.blogsUrl)
.subscribe(res => {
this.blogArray = res;
console.log('theBlogArray: ', this.blogArray);
}, Err => console.log(Err));
}
.
// Service
import 'rxjs/add/operator/catch';
return this.http.get(url)
.map((response) => response.json())
.catch(Err);
ngOnInit() {
this.blogService.getBlogs(this.blogsUrl)
.subscribe(res => {this.blogArray = res;
console.log('theBlogArray: ', this.blogArray);// this will work and use it in template like{{blogArray}}
);
}
您正在获取数据,唯一的事情是您正在控制台将其记录到外部,因为它是异步调用,您无法在同步上下文中登录。
更新
我使用 angular 尝试了同样的 url 它给出了一些 CORS 问题后端有问题请检查控制台。你会得到错误
我没有看到来自 http 请求的响应的原因是因为我在主应用程序组件中引用了 blog-list 组件,而没有在主应用程序组件中导入 blog-list。
我在做这个
<app-nav-bar></app-nav-bar>
<app-blog-list></app-blog-list>
这需要我这样做
import { Component } from '@angular/core';
import { BlogListComponent } from './blog-list/blog-list.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
}
我仍然不确定为什么我能够从服务而不是从 http 请求访问硬编码数组的值,而无需将 blog-list.component 导入主应用程序。
blog.service
public blogExample: Blog;
private loading: boolean;
constructor(private http: Http) {
this.blogExample = {
id: 1,
title: 'title',
content: 'content'
};
}
博客-list.component
ngOnInit() {
this.blogService
.getBlogs(this.blogsUrl)
.subscribe(res => {
this.blogArray = res;
console.log('theBlogArray', this.blogArray);
});
console.log('blogExample', this.blogService.blogExample);
}