![](/img/trans.png)
[英]Angular 2: HTTP Post Request with URL parameters and body type parameter
[英]Angular http post method request with url and no body parameters
打字稿代码应该调用 POST 端点。 终点只是一个没有正文参数的网址。 我已经通过提供 url 而没有其他参数来尝试 http.post() 方法。
示例代码。
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 不可用。 与此绑定解决了该问题。
应该将HTTP注入构造函数中
import {Http} from '@angular/http';
export class Myservice{
constructor(public http: Http) { }
}
上面的代码在回调内部使用,因此此上下文已更新为其他上下文,因此http不可用。 与此绑定可以解决该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.