简体   繁体   English

Angular 4.0 http put请求

[英]Angular 4.0 http put request

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

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);
});
}

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 Tour Of Heroes》 ,它基于angular 2,并且大多数功能都可以在angular 4中使用,其中有一部分专门用于http请求: https : //angular.io/tutorial/toh-pt6

In the second example you are not returning the response within the map, you are returning the human that was originally passed in. 在第二个示例中,您没有返回地图内的响应,而是返回了最初传入的人员。

So, basically you are creating an illusion that it is working, when it isn't. 因此,基本上,您正在创建一种幻觉,即它在起作用,而当它不起作用时。

Probably best to test your API with something like PostMan, to see if you can get it working with that first. 最好用PostMan之类的东西来测试您的API,看看是否可以使其首先与它一起使用。

You use map method incorrectly, read more about this method in documentation: http://xgrommx.github.io/rx-book/content/observable/observable_instance_methods/map.html 您错误地使用了地图方法,请在文档中详细了解此方法: http : //xgrommx.github.io/rx-book/content/observable/observable_instance_methods/map.html

If you want receive response from server your code should look like that: 如果要从服务器接收响应,则代码应如下所示:

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

You can use map method if you want to modify server response(map some objects to other structures etc.): 如果要修改服务器响应(将某些对象映射到其他结构等),可以使用map方法:

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

map method returns Observable object, so you can subscribe that and wait for response, error or simple complete method(third parameter of subscribe()): http://xgrommx.github.io/rx-book/content/observable/observable_instance_methods/subscribe.html map方法返回Observable对象,因此您可以订阅该对象并等待响应,错误或简单的完整方法(subscribe()的第三个参数): http : //xgrommx.github.io/rx-book/content/observable/observable_instance_methods/ subscription.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM