我试图将此测试数据从我的节点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 app中使用了cors模块。
加(我的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": {}
}
}
答案 0 :(得分:1)
根据我所看到的情况,您的路由存在问题,您的快递正在等待"http://localhost:4200/abc"
返回"success!"
,但在您的Angular2中,您试图点击{{1} }。
尝试直接点击你的网址,看看你是否能得到结果,
尝试将其放入浏览器中:
"http://localhost:4200/auth/abc";
如果你收回你的JSON,你的路由器很好,你只需要在你的应用中修复网址。