无法在angular 6中使用HTTPClient提取数据

时间:2018-08-15 18:29:05

标签: angular rest http express

我正在尝试使用带有角度6中的http客户端的API来获取数据 下面是为该服务编写的服务。

getData(): Observable<any> {
  let headers = new HttpHeaders();
  headers  = headers.append('Accept', 'application/json');
  headers  = headers.append('Content-Type', 'application/json');
  let params = new HttpParams();
  params = params.append('table', 'tblDefects');
  return this.http
      .get(this.url, {headers, params })
      .pipe(map(res => res));
}

我还用node和Express js编写了一个API  如下

router.get('/dataset/:table', function(req, res){
  MongoClient.connect(url, function(err, db) {
    if (err) throw err;

    var dbo = db.db("NBCC");

    var query = { BG_STATE: "Active" };
    var col = req.params.table;

    dbo.collection(col).find(query).toArray(function(err, result) {
      if (err) throw err;
      db.close();
      res.send(result)
    });
  });
});

我遇到错误了,

ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":200,"statusText":"OK","url":"http://localhost:3000/api/dataset?table=tblDefects","ok":false,"name":"HttpErrorResponse","message":"Http failure during parsing for http://localhost:3000/api/dataset?table=tblDefects","error":{"error":{},"text":"<!DOCTYPE html><html lang=\"en\"><head>\n  <meta charset=\"utf-8\">\n  <title>NewCommandCenter</title>\n  <base href=\"/\">\n\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\">\n  <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/dc/2.0.0-beta.29/dc.css\">\n               \n\n<!--   <link rel=\"stylesheet\" href=\"./Style/bootstarp.css\">\n  <link rel=\"stylesheet\" href=\"./Style/dc.css\">\n -->\n <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>\n<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script>\n\n\n  <link rel=\"icon\" type=\"image/x-icon\" href=\"favicon.ico\">\n</head>\n<body>\n  <app-root></app-root>\n\n\n<script type=\"text/javascript\" src=\"runtime.js\"></script><script type=\"text/javascript\" src=\"polyfills.js\"></script><script type=\"text/javascript\" src=\"styles.js\"></script><script type=\"text/javascript\" src=\"vendor.js\"></script><script type=\"text/javascript\" src=\"main.js\"></script></body></html>"}}
at resolvePromise (zone.js:814)
at zone.js:724
at rejected (main.js:845)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.js:4071)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (zone.js:138)
at zone.js:872
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4062)

enter image description here

0 个答案:

没有答案