【问题标题】:angular http request doesn't work but on postman works角度http请求不起作用,但邮递员工作
【发布时间】:2019-05-27 00:32:10
【问题描述】:

我正在尝试使用以下 url 发出 http get 请求:

 private materialsURL2='https://localhost:5001/api/material';

setPrice(id: any, price: any): Observable<any> {

  const url = `${this.materialsURL2}/${id}/price/${price}`;


 return this.http.get<any>(url,httpOptions).pipe(
          tap(_ => this.log(`updated material price id=${id}`)),
          catchError(this.handleError<any>('updateMaterialPrice'))
        );
      }

但是什么也没发生。如果我在浏览器中打开网络部分,则没有请求记录。如果我在邮递员中使用相同的 URL,它会正确执行请求。

【问题讨论】:

  • 关注link
  • 确保您在之前打开了“网络”部分,这意味着已经发生(例如,Edge 不会向您显示在您打开选项卡之前运行的网络请求)。这听起来像是一个跨域问题。您的页面是从localhost:5001 提供的吗?如果没有,localhost:5001 是否返回必要的 CORS 标头以允许来自您的页面运行的任何位置的请求?
  • @Abhishek - 盲链接没用。 具体来说,您指的是关于该文档的什么 OP?你看到他们做错了什么吗?如果是这样,请在提供链接时说出那是什么
  • @T.J.Crowder 谢谢你的建议我明白了

标签: javascript angular typescript http angular-http


【解决方案1】:

您似乎从未订阅过获取请求。订阅后该请求将触发。

return this.http.get<any>(url,httpOptions).pipe(
  tap(_ => this.log(`updated material price id=${id}`)),
  catchError(this.handleError<any>('updateMaterialPrice'))
).subscribe((result) => {
  console.log('now it should work', result);
})

https://angular.io/api/common/http/HttpClient#get

【讨论】:

    【解决方案2】:

    缺少对 observable 的订阅。如果不对 observable 调用 subscribe,则底层链不会执行任何操作。

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 2019-08-25
      • 2016-03-09
      • 2021-09-14
      • 2020-11-23
      • 2016-10-23
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多