简体   繁体   English

Angular HTTP 请求获取响应正文

[英]Angular HTTP request get response body

I am currently trying to figure out how to get the response body.我目前正试图弄清楚如何获得响应正文。 I can see it in the browser's network console as an array of strings(this is what i would like to get) but using my code will only return an Object:我可以在浏览器的网络控制台中看到它作为一个字符串数组(这是我想要的),但使用我的代码只会返回一个对象:

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);
            }
          });

It is an POST request.这是一个 POST 请求。

This is the log:这是日志:

resp: [object Object]

You need to use JSON.stringify to see the actual content您需要使用 JSON.stringify 来查看实际内容

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

if you want to access particular field from the response, use JSON.parse to convert to Object如果您想从响应中访问特定字段,请使用 JSON.parse 转换为 Object

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

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

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