简体   繁体   English

从jsonapi加载模型时,ember中止过渡到路由

[英]ember aborts transition to route when loading model from jsonapi

I have a very basic ember application. 我有一个非常基本的余烬应用程序。 It defines a route called "clock" and a model called "timezone". 它定义了一个称为“时钟”的路径和一个称为“时区”的模型。

models/timezone.js: models / timezone.js:

import DS from 'ember-data';

export default DS.Model.extend({
  title: DS.attr(),
  technicalName: DS.attr(),
  utcOffset: DS.attr()
});

routes/clock.js: 路线/clock.js:

import Route from '@ember/routing/route';

export default Route.extend({
  model() {
    return this.get('store').findAll('timezone');
  }
});

The data is loaded from a rest api that returns data according to the json api specification. 数据是从rest api加载的,rest api根据json api规范返回数据。 I am using an application adapter: 我正在使用应用程序适配器:

adapters/application.js: 适配器/application.js:

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
  host: 'http://localhost:3000'
});

The api returns the following data when calling http://localhost:3000/timezones : 调用http:// localhost:3000 / timezones时 ,api返回以下数据:

{"data":
 [
  {
   "type":"timezones",
   "id":"1",
   "attributes": {
    "title":"Local Time",
    "technical-name":"alfa",
    "utc-offset":1
    }
   }
]}

However ember refuses to render the clock route: 但是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

What am I missing here? 我在这里想念什么?

Finally I found the problem. 终于我找到了问题。 It was a Cross-Origin Issue. 这是一个跨域问题。 I solved it by adding 我通过添加解决了

'connect-src': "'self' http://localhost:3000"

the the ember environment config (config/environment.js) and adding the Access-Control headers in the response of my express application: 余烬环境配置(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM