簡體   English   中英

在Angularjs 2 HTTP GET請求中以純文本或xml的形式檢索響應主體

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

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