简体   繁体   English

http.get() 不返回可观察的

[英]http.get() does not return an observable

NOTE: The problem is definetly the ionViewDidLoad() not being executed cause the http.get does get an observable.注意:问题肯定是 ionViewDidLoad() 没有被执行,因为 http.get 确实得到了一个 observable。 I'm trying to get the observable when executing a request in order to get its json response later but I don't get an error, and nothing is displayed in the browser's console either.我试图在执行请求时获取 observable 以便稍后获取其json响应,但我没有收到错误,并且浏览器的控制台中也没有显示任何内容。 I don't know why it does not return anything.我不知道为什么它不返回任何东西。

I have tried many urls and checked many times the imports.我尝试了很多网址并检查了很多次导入。 console.log not pasting the data home.page.ts console.log 没有粘贴数据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 service.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 app.module.ts


    import { ServicioService } from './servicio.service';
    import { HttpClientModule } from '@angular/common/http';

    imports: [HttpClientModule, BrowserModule, IonicModule.forRoot(), AppRoutingModule, ComponentesModule],
      providers: [
        ServicioService,...

I just hope to get an observable to be able to read it and extract specific data from it.我只是希望得到一个 observable 能够读取它并从中提取特定数据。

With Ionic 4, in Angular you no longer have the ionViewDidLoad lifecycle hook.使用 Ionic 4,在 Angular 中你不再拥有ionViewDidLoad生命周期钩子。 Use the standard Angular ngOnInit hook instead.改用标准的 Angular ngOnInit钩子。

See https://medium.com/@paulstelzer/ionic-4-and-the-lifecycle-hooks-4fe9eabb2864https://medium.com/@paulstelzer/ionic-4-and-the-lifecycle-hooks-4fe9eabb2864

Lifecycles in Ionic 4 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除了ionViewDidLoad (因为它和ngOnInit一样)和两个导航守卫,所有来自 Ionic 3 的 Lifecycle 钩子仍然可用

You are doing a get on this URL: ' https://www.json.org '您正在获取此 URL:' https://www.json.org '

There maybe a bunch of things that may be going wrong here.这里可能有很多事情可能会出错。 One of them could be CORS.其中之一可能是 CORS。

If your web app does not allow CORS (cross-origin request sharing), then you will not get response back from the call.The call is essentially blocked by the browser (check your browser).如果您的 Web 应用程序不允许 CORS(跨域请求共享),那么您将无法从调用中获得响应。调用本质上是被浏览器阻止的(检查您的浏览器)。 The first call if you look at the network log will be OPTIONS instead of a get.如果您查看网络日志,第一个调用将是 OPTIONS 而不是 get。 Essentially the website should be returning the Http operation Verbs that it would be allowed to call.本质上,网站应该返回允许调用的 Http 操作动词。

Try calling a local iis server app on your machine or any machine on the same domain as the web app, http.get will work.尝试在您的机器上或与 Web 应用程序位于同一域的任何机器上调用本地 iis 服务器应用程序,http.get 将起作用。

It's seems like you component is missing the http in the constructor and you are trying to call the service in the wrong way.看起来您的组件在构造函数中缺少 http 并且您试图以错误的方式调用该服务。

You don't need to put it inside a veritable in the method.你不需要把它放在方法中的一个名副其实的里面。

**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;
}));

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM