Ember 3.2.2 不将请求路由到 .NET Core 2.1 JSON web API
Ember 3.2.2 not routing request to .NET Core 2.1 JSON web API
我是 Ember 的新手,一直在关注演示如何设置 JSON API 后端与 Ember 前端 - 我正在使用 Visual Studio 代码。我已成功完成第一个视频并收到来自 JSON API 后端的回复。但是,我无法通过让 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
router.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;
environment.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;
};
application.js
import DS from 'ember-data';
import ENV from './config/environment';
export default DS.JSONAPIAdapter.extend({
namespace: ENV.APP.namespace,
host: ENV.APP.host
})
待办事项-items.js
import Route from '@ember/routing/route';
export default Route.extend({
model(){
return this.store.findAll('todo-item');
}
});
模型文件:
待办事项-item.js
import DS from 'ember-data';
const { attr, belongsTo} = DS;
export default DS.Model.extend({
description: attr('string'),
owner: belongsTo('person')
});
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 上 运行。
- API URL 调用:http://localhost:5000/api/v1/people
- Ember URL 调用:http://localhost:4200/todo-items
更新 2
服务器消息:
- API:正在收听:http://localhost:5000
- Ember:服务于 http://localhost:4200/
您 运行 ember 使用指向您后端的代理参数进行服务吗?
尝试在您的终端中运行:
ember s -pr=http://localhost:5000
那么您的请求应该到达正确的端点。
我发现了问题。我需要设置 CORS。
我是 Ember 的新手,一直在关注演示如何设置 JSON API 后端与 Ember 前端 - 我正在使用 Visual Studio 代码。我已成功完成第一个视频并收到来自 JSON API 后端的回复。但是,我无法通过让 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
router.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;
environment.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;
};
application.js
import DS from 'ember-data';
import ENV from './config/environment';
export default DS.JSONAPIAdapter.extend({
namespace: ENV.APP.namespace,
host: ENV.APP.host
})
待办事项-items.js
import Route from '@ember/routing/route';
export default Route.extend({
model(){
return this.store.findAll('todo-item');
}
});
模型文件:
待办事项-item.js
import DS from 'ember-data';
const { attr, belongsTo} = DS;
export default DS.Model.extend({
description: attr('string'),
owner: belongsTo('person')
});
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 上 运行。
- API URL 调用:http://localhost:5000/api/v1/people
- Ember URL 调用:http://localhost:4200/todo-items
更新 2
服务器消息:
- API:正在收听:http://localhost:5000
- Ember:服务于 http://localhost:4200/
您 运行 ember 使用指向您后端的代理参数进行服务吗?
尝试在您的终端中运行:
ember s -pr=http://localhost:5000
那么您的请求应该到达正确的端点。
我发现了问题。我需要设置 CORS。