首页 > 解决方案 > Angular7 Cannot Send Thr Correct Http Request(There is no record in Chrome Network sniffer when 'http.get')

问题描述

I want to send a HTTP GET request within @Angular/common/HttpClint, but there is no response, even chrome network sniffer cannot detect any network activities.

When I tried to console.log, this was the result:

 console.log(this.http.get("http://localhost/users"));

Then I found that the result of console was:

Observable {_isScalar: false, source: Observable, operator: MapOperator}

And chrome network sniffer didn't detect any XHR related to my action.

Why?

Can it be because of using @ng-cli to create this project?

I init this Angular project via @ng-cli.

when I use an example project (Heroes Example), there are no problems.

But each of my project that created by @ng-cil(ng new project) has that problem.

More Info:

Result of the console:

标签: angularobservablehttpclientangular7

解决方案


You have to subscribe to the http.get to get any response.

 console.log(this.http.get("http://localhost/users").subscribe());

From angular docs:

An HttpClient method does not begin its HTTP request until you call subscribe() on the observable returned by that method. This is true for all HttpClient methods.

https://angular.io/guide/http


推荐阅读