[英]ember aborts transition to route when loading model from jsonapi
我有一個非常基本的余燼應用程序。 它定義了一個稱為“時鍾”的路徑和一個稱為“時區”的模型。
models / timezone.js:
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr(),
technicalName: DS.attr(),
utcOffset: DS.attr()
});
路線/clock.js:
import Route from '@ember/routing/route';
export default Route.extend({
model() {
return this.get('store').findAll('timezone');
}
});
數據是從rest api加載的,rest api根據json api規范返回數據。 我正在使用應用程序適配器:
適配器/application.js:
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
host: 'http://localhost:3000'
});
調用http:// localhost:3000 / timezones時 ,api返回以下數據:
{"data":
[
{
"type":"timezones",
"id":"1",
"attributes": {
"title":"Local Time",
"technical-name":"alfa",
"utc-offset":1
}
}
]}
但是ember拒絕提供時鍾路線:
Attempting URL transition to /clock
Transition #0: application: calling beforeModel
Transition #0: application: calling deserialize hook
Transition #0: application: calling afterModel hook
Transition #0: clock: calling beforeModel hook
Transition #0: clock: calling deserialize hook
Transition #0: clock: transition was aborted
我在這里想念什么?
終於我找到了問題。 這是一個跨域問題。 我通過添加解決了
'connect-src': "'self' http://localhost:3000"
余燼環境配置(config / environment.js),並在我的快速應用程序的響應中添加Access-Control標頭:
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
next();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.