简体   繁体   English

Angular:发送带有空请求正文的 POST 请求

[英]Angular: Sending POST request with empty request body

I am making a angular POST call to a backend service.我正在对后端服务进行 angular POST 调用。 Usecase is such that request has no body, so I do like:用例是这样的,请求没有正文,所以我喜欢:

http.post("my/backend/service", null);

I observe that endpoint is not hit, and there is also no error.我观察到端点没有被命中,也没有错误。

Is this not the correct way of handling empty request body for POST?这不是处理 POST 空请求正文的正确方法吗?

post method return an Observable so you need either to subscribe to the post method or return the Observable and subscribe to your method. post方法返回一个Observable ,因此您需要subscribe post方法或返回Observablesubscribe您的方法。 Calling the post method and getting the Observable is like preparing the request, subscribing to the Observable is like runing the query.调用post方法并获取Observable就像准备请求一样,订阅 Observable 就像运行查询一样。

http.post("my/backend/service", null).subscribe();

You can find more about it in the last section of this Angular tutorial .您可以在本 Angular 教程的最后一节中找到有关它的更多信息。

Because the service method returns an Observable of configuration data, the component subscribes to the method's return value.因为服务方法返回一个配置数据的 Observable,所以组件订阅了该方法的返回值。

The example use the get() method, but the problem come from the type of the response: Observable .该示例使用get()方法,但问题来自响应的类型: Observable

Send empty JSON object, May be it will work.发送空的 JSON object,可能会起作用。

http.post("my/backend/service", {});  

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

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