【问题标题】:Ember js Error: Adapter operation failedEmber js 错误:适配器操作失败
【发布时间】:2015-12-23 03:37:37
【问题描述】:

作为 Ember.js 的初学者,我尝试从我的 Rails Api 服务器获取一些数据到 Ember 应用程序。我的 Ember 应用程序正在尝试获取并显示从 rails api 提供的类别名称。 我正在使用

  • Ember 版本:1.13.8
  • 节点:0.12.7
  • npm: 2.13.4

app/router.js

Router.map(function() {
  this.resource('categories',function(){
    this.resource('category',{path: '/:category_id'});
  });
});

路由/类别/index.js

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

app/models/category.js

export default DS.Model.extend({
    name: DS.attr('string')
});

app/adapters/application.js

export default DS.JSONAPIAdapter.extend({
   shouldReloadAll: function() { return true; },
   namespace: 'v1',
   host: 'http://ip.address-to_rails_api'
});

app/templates/categories/index.hbs

<ul>
    {{#each model as |category|}}
       <li>{{category.name}}</li>
    {{/each}}
 </ul>

现在,当我在浏览器中访问 http://ip.address-to_rails_api 时,我会收到响应

{"categories":[{"id":1,"name":"Entertainment"}, {"id":2,"name":"Education"}]}

但是当我在我的 ember 应用程序中访问 /categories 时,即http://localhost:4200/categories 注意显示在浏览器中,我在 ember 检查器中收到错误

routeName: "categories.index_error"
context: Error: Adapter operation failed
currentModel: Error: Adapter operation failed

ember 服务器控制台也反映错误

Content Security Policy violation: {}

我也尝试将 JSONAPIAdapter 更改为 RESTAdapter,但它给出了弃用错误。请帮我理解问题。

【问题讨论】:

  • 我在这里遇到了同样的问题stackoverflow.com/questions/32670533/…。不幸的是,我现在没有解决方案
  • 需要注意的一点是,我认为“资源”在 1.13.8 之前已被弃用。
  • 是的,我将资源更改为路由,但问题出在其他问题上。 ember 引起的主要问题是由于代码的指数变化。他们正在以非常快的速度对其进行更改,因此每个东西都会在一个月后被弃用。

标签: javascript ruby-on-rails ember.js ember-data ember-cli


【解决方案1】:

终于解决了问题 问题出在 Rails API 和 Ember App 两部分

第一个错误

Content Security Policy violation: {}

已通过向 ember 应用添加内容安全策略来移除 配置/环境.js

contentSecurityPolicy: {
  'default-src': "'none'",
  'font-src': "'self'",
  'img-src': "'self'",
  'media-src': "'self'",
  'style-src': "'self' 'unsafe-inline'",
  'script-src': "'self' 'unsafe-eval' http://ip_to_rails_api",
  'connect-src': "'self' ws://ip_to_rails_api"
}

然后我得到一个错误

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 404.

此错误已通过在 rails api 端使用跨域资源共享或 CORS 得到解决。这样对于每个请求,浏览器首先发送一个 OPTIONS 请求,然后服务器返回一个带有一些额外标头的响应。然后浏览器就可以发出原始请求了。

有关 CORS 的更多信息,请参阅 How does Access-Control-Allow-Origin header work?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多