Angular 4-HTTP删除不起作用 [英] Angular 4 - HTTP delete not working

查看:73
本文介绍了Angular 4-HTTP删除不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个MEAN作业.我在节点上有一个api,其删除http方法为localhost:3000/api/user?id=<some document id>.下面是我使用的角度代码:

I am trying to create a MEAN crud operation. I have an api in node with the delete http method as so localhost:3000/api/user?id=<some document id>. Below is the angular code I use:

deleteUser(user) {
    console.log("user to delete:" + user._id);
    let myParams = new URLSearchParams();
    myParams.append('id', user._id);
    return this.http.delete('/api/user', { search: myParams })
      .map(res => res.json());
}

正确的ID正在打印到控制台,但是我什至看不到chrome网络栏中的通话,也没有删除数据.任何想法出了什么问题吗?

The correct id is being printed to console but I am not able to see even the call being made in the chrome network bar nor has the data being deleted. Any ideas what is going wrong?

推荐答案

如果要执行该呼叫,则必须订阅该呼叫.请参见 HttpClient文档.

You have to subscribe to the call if you want it to execute. See the HttpClient documentation.

请注意subscribe()方法.从HttpClient返回的所有Observable都是冷的,也就是说,它们是发出请求的蓝图.除非您调用subscribe(),否则什么都不会发生,并且每次这样的调用都会发出一个单独的请求.例如,此代码两次发送具有相同数据的POST请求:

Note the subscribe() method. All Observables returned from HttpClient are cold, which is to say that they are blueprints for making requests. Nothing will happen until you call subscribe(), and every such call will make a separate request. For example, this code sends a POST request with the same data twice:

示例:

otherMethod(){
    this.userService.deleteUser(user).subscribe(() => console.log("user deleted"));
}

这篇关于Angular 4-HTTP删除不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆