angular - 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:
解决方案
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.
推荐阅读
- uipath - UiPath 中的 IF 条件
- git - 如何从远程仓库拉到远程分支
- android - Android 资源链接失败 - Android Studio
- click - 通过向元素动态添加“单击”事件打开 MatDialog
- javascript - 搜索匹配的单元格,然后将一个单元格复制到 Google 表格中的其他位置
- snowflake-cloud-data-platform - 将空字符串作为空字符串(非空)从 Snowflake 下载到 Databricks
- python - 如何将csv导出到python中的服务器位置?
- python - 推理时的单幅图像特征缩减
- python - 使用 zeromq 时在 '__init__.py 中找不到引用
- reactjs - 当设置数组不保持旧状态时,React hooks useState