无法从HTTP GET请求Nodejs和Angular2获取数据

时间:2018-01-29 09:43:25

标签: http angular2-routing nodes mean-stack

我正在尝试从本地数据库中获取数据。 本地网址: http://localhost:8000/

我在我的服务中强调了这个 / getPersonalInfoData 状态显示200,但我看不到任何数据。

component.ts:

var data = this.personalInfoService.getPersonalInfoData()
            .subscribe(arg => this.driverData = arg);
            console.log(data);

personalInfoService:

return this.http.get('/getPersonalInfoData').map((res: Response) => { console.log(res); return res; });

后端数据源API(Nodejs):

routes.get('/getPersonalInfoData',personal_info_controller.getPersonalInfoData);

1 个答案:

答案 0 :(得分:1)

在personalInfoService中: -

1.Import { Http, Response, Headers, RequestOptions, URLSearchParams}来自'@angular/http';

2.在方法中添加以下代码:

const headers = new Headers({
      'Content-Type': 'application/json',
      'Cache-control': 'no-cache',
      Expires: '0',
      Pragma: 'no-cache'
    });
    const options = new RequestOptions({ headers: headers });
    return this.http.get(Url, options).map(res => {
      return res.json();
    });

希望这会对你有所帮助。

相关问题