繁体   English   中英

Angular HTTP 请求获取响应正文

[英]Angular HTTP request get response body

我目前正试图弄清楚如何获得响应正文。 我可以在浏览器的网络控制台中看到它作为一个字符串数组(这是我想要的),但使用我的代码只会返回一个对象:

this.http.request(req).subscribe(event => {
            if (event.type === HttpEventType.UploadProgress) {
                const percentDone = Math.round(100 * event.loaded / event.total);
                console.log(`File is ${percentDone}% uploaded.`);
            } else if (event instanceof HttpResponse) {
                console.log('File is completely uploaded!');
                console.log('resp: ' + event.body);
            }
          });

这是一个 POST 请求。

这是日志:

resp: [object Object]

您需要使用 JSON.stringify 来查看实际内容

 console.log('resp: ' + JSON.stringify(event.body));

如果您想从响应中访问特定字段,请使用 JSON.parse 转换为 Object

let filename = (JSON.parse(event.body)).fileName;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM