簡體   English   中英

帶有 url 且沒有正文參數的 Angular http post 方法請求

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM