【问题标题】:hello, im getting the following error when making a http delete request您好,我在发出 http 删除请求时收到以下错误
【发布时间】:2020-12-08 22:32:33
【问题描述】:

我有一个错误:

core.js:6185 ERROR HttpErrorResponse {headers: HttpHeaders, status: 404, statusText: "Not Found", url: "http://localhost:2000/delete-post/[object%20Object]", ok:错误,...}

代码:

public deletePost( _id: string) { return this.webService.delete(delete-post/${_id}); }

【问题讨论】:

  • 通常你只发送一个“simple”参数来删除一个dbs的寄存器,检查你对api的调用,必须像(你给我们不好的线索):httpClient.delete('delete-post/"+data.id)httpClient.get('deletepost/'+data.id)httpClient.post('delete-post',data)。看到删除并在 url 中传递对象的属性并发布整个对象,但不在 url 中
  • public deletePost(_id: string) { return this.webService.delete(delete-post/${_id}); }

标签: angular http mean-stack


【解决方案1】:

您将整个对象放在 url 中

你必须有类似的东西

httpClient.delete(`http://localhost:2000/delete-post/${item}`)

当你真正需要时

httpClient.delete(`http://localhost:2000/delete-post/${item.id}`)

基本上,您希望将项目的id 连接到该 url 字符串,而不是整个项目

【讨论】:

  • 感谢博格丹抽出宝贵时间。这是我的代码: public deletePost(_id: string) { return this.webService.delete(delete-post/${_id}); }
  • 问题是当你使用整个对象时你通常会得到[object%20Object]。请创建 console.log() 并确保 _idstring 而不是整个对象
  • 我把 id 作为一个整体来获取
  • 那么你的方法应该是这样的public deletePost(data: any) { return this.webService.delete(delete-post/${data.id});}
猜你喜欢
  • 2015-12-04
  • 2018-05-29
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 2012-10-04
相关资源
最近更新 更多