[英]Retrieve Response body as plain text or xml in Angularjs 2 HTTP GET request
我正在嘗試向返回XML的服務器發出get
請求:
let text = "";
this.http.get('http://example.com', {headers : headers})
.map((res:Response) => res.text()).subscribe(data => text = data);
但是, text
變量是空字符串,如何檢索純文本以轉換為XML或如何直接獲取XML?
您的代碼對我來說非常合適,也許您在http調用完成之前嘗試訪問text
? 請記住,http調用是異步操作。 試試這個,你會發現它完美無缺:
let text = "";
this.http.get('https://jsonplaceholder.typicode.com/posts')
.map((res:Response) => res.text())
.subscribe(
data => {
text = data;
console.log(text);
});
你的方法很好看。 試試這樣吧
this.http.get('http://example.com', {headers : headers})
.map((res:Response) => { return res.text() }).subscribe(data => {text = data});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.