Angular 4.0 http put请求 [英] Angular 4.0 http put request

查看:315
本文介绍了Angular 4.0 http put请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个函数来发送http put请求以更新一些数据,但是它说,它没有接收到任何数据:

I've written a function to send a http put request to update some data but it says, that it is not recieving any data:

updateHuman(human: Human) {
    const url = `${this.url}/${human.id}`;
    const data = JSON.stringify(human);
    return this.http.put(url, data).map(
        response => response.json().data as Human,
        error => console.log(error)
    );
}

将功能更改为以下功能后,它可以正常工作:

After I've changed my function to the following, it is working:

updateHuman(human: Human) {
    const url = `${this.url}/${human.id}`;
    const data = JSON.stringify(human);
    return this.http.put(url, data).map(() => human);
}

有人可以解释一下,为什么第一个功能不起作用,而第二个功能却起作用吗?

Could someone explain me, why the first function is not working but second is working?

推荐答案

可观察对象是惰性的,您需要订阅它们才能使它们工作并检索任何内容.您订阅了您的方法吗?示例:

Observables are lazy, you need to be subscribed to them for them to work and retrieve anything. Did you subscribe to your method? Example:

methodToUpdateHuman(human): void{
...
this.updateHuman(human).subscribe((response) => {
   //do something with the response
   console.log.("Response is: ", response);
},
(error) => {
   //catch the error
   console.error("An error occurred, ", error);
});
}

我建议您通读英雄角度游,它基于角度2和大多数功能在angular 4中起作用,有专门用于http请求的部分: https://angular.io/tutorial/toh-pt6

I suggest you read through the Angular Tour Of Heroses, it's based in angular 2 and most of the functionality is functional in angular 4, there is a section dedicated to http requests: https://angular.io/tutorial/toh-pt6

这篇关于Angular 4.0 http put请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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