Angular HTTP 请求获取响应正文
Angular HTTP request get response body
我目前正在研究如何获取响应正文。我可以在浏览器的网络控制台中看到它作为一个字符串数组(这是我想要得到的)但是使用我的代码只会 return 一个对象:
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 转换为对象
let filename = (JSON.parse(event.body)).fileName;
我目前正在研究如何获取响应正文。我可以在浏览器的网络控制台中看到它作为一个字符串数组(这是我想要得到的)但是使用我的代码只会 return 一个对象:
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 转换为对象
let filename = (JSON.parse(event.body)).fileName;