【问题标题】:400 Bad Request in angular in Http Get request400 Http Get请求中的角度错误请求
【发布时间】:2020-05-09 00:49:11
【问题描述】:

当我直接尝试这个 URL 时:http://dummy.restapiexample.com/api/v1/employee/1 我会得到响应:

但是当我尝试从我的 Angular 应用程序访问相同的 URL 时,它给出了 400 Bad Request。

服务中:

getEmpAPI = 'http://dummy.restapiexample.com/api/v1/employee'

getEmployee(id: string) {
    return this.http.get(`${this.getEmpAPI}/${id}`)
  }

在组件中:

onClick(evt: any, index) {
evt.preventDefault();
let id = index + 1;
this.empDataService.getEmployee(id).subscribe(
  (emp) => {
    console.log(emp);        
  }
)

}

我所拥有的 API 页面仅供参考。 http://dummy.restapiexample.com/

请帮忙:

【问题讨论】:

  • 请大家帮忙!!
  • 发现它的get请求应该是这样的:/employee/{id} GET JSON
  • 如果您从浏览器中删除 cookie 并直接在浏览器中重试 url,则另当别论。它会给出错误 400,但是一旦你刷新相同的 url,数据就会开始出现。这意味着在第一次点击时,在浏览器 cookie 中设置了一些内容,然后再次刷新数据开始出现,因为现在设置了 cookie。但我不知道如何用角度代码处理这个东西。

标签: javascript angular rest


【解决方案1】:

您确定有 id 为 0 的员工吗?在上面的示例中,您使用的是 1

【讨论】:

  • 对不起,我更新了问题是一些错误的编辑。检查知道
猜你喜欢
  • 1970-01-01
  • 2016-11-18
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 2015-04-10
  • 1970-01-01
  • 2017-07-21
相关资源
最近更新 更多