http.get() 不 return 一个可观察的
http.get() does not return an observable
注意:问题显然是 ionViewDidLoad() 没有被执行,因为 http.get 确实得到了一个可观察的。
我试图在执行请求时获取可观察对象,以便稍后获得其 json 响应,但我没有收到错误,并且浏览器中没有显示任何内容控制台。我不知道为什么它没有 return 任何东西。
我试过很多网址,也检查过很多次导入。
console.log not pasting the data
home.page.ts
import { ServicioService } from '../servicio.service';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
...
ionViewDidLoad(){
const hola = this.servicio.goWiki(this.userInput).
subscribe((response) => {
console.log(response);
});
}
servicio.service.ts
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class ServicioService {
constructor(public http: HttpClient) {
console.log('goin!');
}
goWiki(userInput): Observable<any>{
return this.http.get('https://www.reddit.com/r/gifs/top/.json?limit=105sort=hot');
}
app.module.ts
import { ServicioService } from './servicio.service';
import { HttpClientModule } from '@angular/common/http';
imports: [HttpClientModule, BrowserModule, IonicModule.forRoot(), AppRoutingModule, ComponentesModule],
providers: [
ServicioService,...
我只是希望得到一个 observable 能够读取它并从中提取特定数据。
你正在做这个URL:'https://www.json.org'
这里可能有很多地方出了问题。其中之一可能是 CORS。
如果您的 Web 应用程序不允许 CORS(cross-origin 请求共享),那么您将不会从 call.The 调用中得到响应,实际上已被浏览器阻止(检查您的浏览器)。如果您查看网络日志,第一个调用将是 OPTIONS 而不是 get。本质上,网站应该返回允许调用的 Http 操作动词。
尝试调用您机器上的本地 iis 服务器应用程序或与 Web 应用程序位于同一域中的任何机器,http.get 会起作用。
您的组件似乎在构造函数中缺少 http,您正试图以错误的方式调用该服务。
你不需要把它放在一个真正的方法里面。
使用 Ionic 4,在 Angular 中您不再拥有 ionViewDidLoad
生命周期挂钩。请改用标准 Angular ngOnInit
挂钩。
见https://medium.com/@paulstelzer/ionic-4-and-the-lifecycle-hooks-4fe9eabb2864
Lifecycles in Ionic 4
[...]
Except ionViewDidLoad
(because it’s the same like ngOnInit
) and the two nav guards all Lifecycle hooks from Ionic 3 are still available
**service.ts**
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
constructor(public http: HttpClient) { }
getFirst(): Observable<any> {
return this.http.get('https://www.reddit.com/r/gifs/top/.json?limit=105sort=hot').pipe(map(res => {
return res;
}));
}
注意:问题显然是 ionViewDidLoad() 没有被执行,因为 http.get 确实得到了一个可观察的。 我试图在执行请求时获取可观察对象,以便稍后获得其 json 响应,但我没有收到错误,并且浏览器中没有显示任何内容控制台。我不知道为什么它没有 return 任何东西。
我试过很多网址,也检查过很多次导入。 console.log not pasting the data home.page.ts
import { ServicioService } from '../servicio.service';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
...
ionViewDidLoad(){
const hola = this.servicio.goWiki(this.userInput).
subscribe((response) => {
console.log(response);
});
}
servicio.service.ts
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class ServicioService {
constructor(public http: HttpClient) {
console.log('goin!');
}
goWiki(userInput): Observable<any>{
return this.http.get('https://www.reddit.com/r/gifs/top/.json?limit=105sort=hot');
}
app.module.ts
import { ServicioService } from './servicio.service';
import { HttpClientModule } from '@angular/common/http';
imports: [HttpClientModule, BrowserModule, IonicModule.forRoot(), AppRoutingModule, ComponentesModule],
providers: [
ServicioService,...
我只是希望得到一个 observable 能够读取它并从中提取特定数据。
你正在做这个URL:'https://www.json.org'
这里可能有很多地方出了问题。其中之一可能是 CORS。
如果您的 Web 应用程序不允许 CORS(cross-origin 请求共享),那么您将不会从 call.The 调用中得到响应,实际上已被浏览器阻止(检查您的浏览器)。如果您查看网络日志,第一个调用将是 OPTIONS 而不是 get。本质上,网站应该返回允许调用的 Http 操作动词。
尝试调用您机器上的本地 iis 服务器应用程序或与 Web 应用程序位于同一域中的任何机器,http.get 会起作用。
您的组件似乎在构造函数中缺少 http,您正试图以错误的方式调用该服务。
你不需要把它放在一个真正的方法里面。
使用 Ionic 4,在 Angular 中您不再拥有 ionViewDidLoad
生命周期挂钩。请改用标准 Angular ngOnInit
挂钩。
见https://medium.com/@paulstelzer/ionic-4-and-the-lifecycle-hooks-4fe9eabb2864
Lifecycles in Ionic 4
[...]
Except
ionViewDidLoad
(because it’s the same likengOnInit
) and the two nav guards all Lifecycle hooks from Ionic 3 are still available
**service.ts**
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
constructor(public http: HttpClient) { }
getFirst(): Observable<any> {
return this.http.get('https://www.reddit.com/r/gifs/top/.json?limit=105sort=hot').pipe(map(res => {
return res;
}));
}