简体   繁体   English

angular http 客户端发布缺少响应

[英]angular http client post missing response

I have a method in my angular 8 app like this:我的 angular 8 应用程序中有一个方法,如下所示:

this.http.post(url, body, options).subscribe(
  res => {
    console.log('here')
    console.log(res)
  }
)

If the backend is setup like this:如果后端是这样设置的:

res.status(201).send({ msg: 'User created' });

The block inside the subscribe call gets executed. subscribe 调用中的块被执行。

However, if the backend is setup like this:但是,如果后端设置如下:

res.status(201).send('User created');

The block inside the subscribe call doesn't get executed (even though the backend processed the request). subscribe 调用中的块不会被执行(即使后端处理了请求)。

Why is this so?为什么会这样?

Angular http call expects object from backend while you are sending a string.当您发送字符串时,Angular http 调用需要来自后端的 object。 Use response type in http options to get string in response在 http 选项中使用响应类型来获取响应字符串

https://angular.io/guide/http#requesting-a-typed-response https://angular.io/guide/http#requesting-a-typed-response

You are missing the parenthesis in the Subscribe.您在订阅中缺少括号。

. . subscribe (res => {})订阅 (res => {})

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

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