自从我从 in-memory-web-api URL 切换到数据库 URL 后,Angular 应用的 JavaScript 控制台就会出现 404 错误
JavaScript console output 404 error
我已经使用 localhost:3035/在 lite-server 上运行了我的 Angular 应用程序
我有一个在 localhost:3039/上运行的 mongodb/nodejs/express 数据库
我的 Angular 应用程序使用内存中的 web-api url“api/loggerData”“GET”就很好
有什么想法吗?是 CORS 吗?还有别的事吗?我是否还需要在 Angular 端配置 lite-server?
这是我的 Angular 代码:
private loggerUrl = 'localhost:3039/read/getall/';
getLoggerData(): Promise <Dataset[]> {
return this.http.get(this.loggerUrl)
.toPromise()
.then(response => response.json().data as Dataset[])
.catch(this.handleError);
}
另外,我尝试在数据库端实现一些 CORS 解决方案,但没有任何影响 -
这是我修改的一些数据库代码:
var app = express();
app.use(function (req, res, next) {
//Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3035');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods',
'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
最佳答案
No collection
错误通常意味着,您已经悬置了一些相当于:
InMemoryWebApiModule.forRoot(InMemoryDataService)
在你的 ngModule 中,当你尝试发出“真正的”http 请求时,它会干扰。因此,从您的 ngModule 中删除它,您就可以开始了! :)
在此之后,您可能仍然会遇到 CORS 问题,但这应该可以修复您遇到的当前错误:)
正如 echonax 所提到的,您应该使用完整的 url,其中包含 http:
private loggerUrl = 'http://localhost:3039/read/getall/'
关于node.js - 当使用来自不同本地主机端口的 GET api 时,本地主机上的 Angular 收到 404 - CORS 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43078340/