HttpEvent进度后如何显示JSON HttpResponse?

时间:2018-07-12 03:59:09

标签: angular httpresponse

在此处遵循教程Listening to Progress Events

我想问问response进行之后是否可以从服务器返回HttpEvent

这是我的示例代码。

const req = new HttpRequest('POST', url, formData, {
  reportProgress: true,
  responseType: 'json',
});

getEventMessage(event: HttpEvent<any>) {
  switch (event.type) {
    case HttpEventType.Sent:
      return 'Uploading data...'

    case HttpEventType.UploadProgress:
      const percentDone = Math.round(100 * event.loaded / event.total)
      return `is ${percentDone} uploaded.`

    case HttpEventType.Response:
       return `was completely uploaded. ` + event.body; // I want to show the json response from server.
      //return HttpEventType.Response;

    default:
      // code...
      return `surprising upload event: ${event.type}.`;
  }
}

this.http.request(req).pipe(
  map(event => console.log(this.getEventMessage(event))),
  tap(message => console.log('Message: '+ message)), // message undefined
  last(),
).subscribe(response => {
    console.log('RESPONSE: ', response); // It returns undefined
})

我想显示来自服务器的json响应。说真的,我对如何做一无所知。任何帮助表示赞赏。 谢谢!

0 个答案:

没有答案