http - Angular2 http 返回空响应?

标签 http angular typescript angular2-http

我有一个后端 API,如 POSTMAN 屏幕截图所示,它正在运行。 enter image description here

这是我的服务:

getComments(line: string, department: string, startTime?: number, endTime?: number): Observable<Array<IComments>> {
    let headers = new Headers(
        {
            'Content-Type': 'application/x-www-form-urlencoded',
            'line': line,
            'cell': department,
            'start': new Date(),
            'end': ago(24, "hours")
        });
    let options = new RequestOptions({ headers: headers });
    return this.http.get('api/data/comments', options)
        .map(this.extractData)
        .catch(this.handleError);
}
private extractData(res: Response) {
    let body = res.json();
    return body.data || {};
}

这是我的组件包含的内容:

  this.dataService.getComments('604', 'powders').forEach(result => {
      console.log('Res: ' + JSON.stringify(result));
      that.comments = result;
  });

但是,我在控制台中得到的是:

Res: {}

为什么没有返回数据,就像在 postman 中一样?

更新:

如果我在 extractData 中 console.log(body),那么我会得到:

Object {result: "COMMENTS604"}

最佳答案

extractData 中,您将返回 body.data,但由于属性名为 result,我认为您的意思是 return正文.result

关于http - Angular2 http 返回空响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41076944/

相关文章:

typescript - 在 typescript 中检查一个也可以为空的值是否大于 4

angular - 对 decorators visual studio 2017 的实验性支持

asp.net - 在 ASP.NET 5 响应中设置 Content-Length header

angular - JHipster : Receive 401 Unauthorized when making API call to microservice

node.js - 为什么 npm install -g @angular/cli?

javascript - 如何在 Angular2 应用程序中使用 MediaRecorder 对象?

http - 在单个 Vary header 中列出多个字段是否有效?

perl - 如何为基本的 http 身份验证设置用户名和密码?

c# - 在 Restsharp 中获得与在 Postman 中不同的响应

angular - NRWL 中的多个应用程序可以访问定义文件的位置