Angular2 http 获取请求 returns html

Angular2 http get request returns html

我正在尝试将此测试数据从我的节点 js 服务器发送到前端。

router.get('/abc', (req, res) => {
  return res.json({
   test: "success!"
  });
});

在我的服务组件中:

private url = "http://localhost:4200/auth/abc";

getTitles() {
    return this.http.get(this.url)
    .map(res => res.json());
}

我期望的是我的测试对象,但最终得到了我的 index.html 字符串格式的文件:(这就是我使用 console.log(res) 时得到的)。

<"!DOCTYPE html">...<"/html">

仅供参考,我在节点 js 应用程序中使用 cors 模块。

plus(我的 angular-cli.json 文件):

 {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "angular-app"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "component": {}
  }
}

你的路由问题,根据我所见,你的快递正在等待 "http://localhost:4200/abc" 到 return "success!" ,但在你的 Angular2 中你正试图点击"http://localhost:4200/auth/abc";.

试试直接打你的url看看能不能得到结果,

尝试将其放入浏览器:

  http://localhost:4200/abc

如果你拿回你的 JSON ,你的路由器是好的,你只需要在你的应用程序中修复 url。