我刚开始使用Ember,并且一直在遵循一个在线视频教程(请参阅下面的Weblink,尽管它使用.NET Core 1.0时已经过时了),它演示了如何使用Ember前端设置JSON API后端-我使用的是Visual Studio代码。我已经成功地完成了第一个视频,并从JSONAPI后端接收了响应。但是,我无法通过让Ember向API后端发送一个数据检索请求来让第二个视频工作。我知道这是因为我正在监视对服务器的调用。所以,虽然我可以点击后端服务器并接收JSON响应,但是前端响应是HTTP错误404-找不到页面,并且没有对后端的请求。
HTTP错误:
Error: Ember Data Request GET /todo-items returned a 404
Payload (text/html; charset=utf-8)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /todo-items</pre>
</body>
</html>
我的最佳猜测是,对于视频中未涉及的路由,对.NET Core和Ember进行了更改。不幸的是,404错误很少发生,我找不到问题。有人知道问题在哪里,或者我如何解决这个问题?
视频教程:
https://www.youtube.com/watch?v=_d53rG2i9pY&index=2&list=PLu4Bq53iqJJAo1RF0TY4Q5qCG7n9AqSZf
路由器.js
import EmberRouter from '@ember/routing/router';
import config from './config/environment';
const Router = EmberRouter.extend({
location: config.locationType,
rootURL: config.rootURL
});
Router.map(function() {
this.route('todo-items');
});
export default Router;
环境.js
'use strict';
module.exports = function(environment) {
let ENV = {
modulePrefix: 'todo-list-client',
environment,
rootURL: '/',
locationType: 'auto',
EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
// e.g. 'with-controller': true
},
EXTEND_PROTOTYPES: {
// Prevent Ember Data from overriding Date.parse.
Date: false
}
},
APP: {
host: 'http://localhost:5000',
namespace: 'api/v1'
}
};
if (environment === 'development') {
// ENV.APP.LOG_RESOLVER = true;
// ENV.APP.LOG_ACTIVE_GENERATION = true;
// ENV.APP.LOG_TRANSITIONS = true;
// ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
// ENV.APP.LOG_VIEW_LOOKUPS = true;
}
if (environment === 'test') {
// Testem prefers this...
ENV.locationType = 'none';
// keep test console output quieter
ENV.APP.LOG_ACTIVE_GENERATION = false;
ENV.APP.LOG_VIEW_LOOKUPS = false;
ENV.APP.rootElement = '#ember-testing';
ENV.APP.autoboot = false;
}
if (environment === 'production') {
// here you can enable a production-specific feature
}
return ENV;
};
应用程序.js
import DS from 'ember-data';
import ENV from './config/environment';
export default DS.JSONAPIAdapter.extend({
namespace: ENV.APP.namespace,
host: ENV.APP.host
})
TODO项目.js
import Route from '@ember/routing/route';
export default Route.extend({
model(){
return this.store.findAll('todo-item');
}
});
模型文件:
TODO项目.js
import DS from 'ember-data';
const { attr, belongsTo} = DS;
export default DS.Model.extend({
description: attr('string'),
owner: belongsTo('person')
});
人.js
import DS from 'ember-data';
const { attr, hasMany} = DS;
export default DS.Model.extend({
firstName: attr('string'),
lastName: attr('string'),
todoItems: hasMany('todo-item')
});
更新1:
api服务器在端口5000上运行,而ember在4200上运行。
更新2
服务器消息: