【发布时间】: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