【问题标题】:Angular http post method request with url and no body parameters带有 url 且没有正文参数的 Angular http post 方法请求
【发布时间】:2017-10-29 20:17:03
【问题描述】:

打字稿代码应该调用一个 POST 端点。终点只是一个没有正文参数的 url。我尝试了 http.post() 方法,只提供 url 而没有其他参数。

示例代码。

let postUrl = "http://hostname:9893/service/fetch/72637"; 

this.http.post(postUrl, {}).map((res:Response) => res.json()).subscribe( data => {
      console.log("post url: "  + data );
});

错误:

core.es5.js:1020 message:"Cannot read property 'post' of undefined" stack:"TypeError: Cannot read property 'post' of undefined\n    at HTMLTableRowElement.<anonymous> (http://localhost:3000/5.chunk.js:222:23)\n    at HTMLTableRowElement.wrapFn [as __zone_symbol___onclick] (http://localhost:3000/polyfills.bundle.js:6601:39)\n    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:3000/polyfills.bundle.js:5969:31)\n    at Object.onInvokeTask (http://localhost:3000/vendor.bundle.js:66347:33)\n    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:3000/polyfills.bundle.js:5968:36)\n    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (http://localhost:3000/polyfills.bundle.js:5736:47)\n    at HTMLTableRowElement.ZoneTask.invoke (http://localhost:3000/polyfills.bundle.js:6031:38)"
__proto__:Error {constructor: , name: "TypeError", message: "", …} constructor:function TypeError() { … }


更新:
解决方案:

上面的代码在回调中使用,所以这个上下文被更新为不同的上下文,因此 http 不可用。绑定 this 解决了这个问题。

【问题讨论】:

    标签: angular http-post angular-http


    【解决方案1】:

    上面的代码在回调中使用,所以这个上下文被更新为不同的上下文,因此 http 不可用。绑定 this 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      相关资源
      最近更新 更多