为什么 ember 没有对 REST 请求使用正确的主机

Why isn't ember using the correct host for REST requests?

本文关键字:主机 请求 ember REST 为什么      更新时间:2023-09-26

我正在尝试使用余烬进行一个简单的 REST API 设置,但我无法让它达到正确的终点,我已经尝试了各种方法都无济于事,但我的代码目前看起来像这样:

window.App = Ember.Application.create({
    LOG_TRANSITIONS: true,
    LOG_TRANSITIONS_INTERNAL: true
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://myserver.com'
});
/* Setup routes */
App.Router.map(function() {
  this.route("bar");
});
App.BarRoute = Ember.Route.extend({
    model: function() {
        return this.store.findAll('bar');
    }
});

在主索引模板中,我{{#link-to "bar"}}Bars{{/link-to}},但是当我单击链接(在本地运行页面)时,我在控制台中看到以下内容:

OPTIONS file://localhost/bars  

无论我尝试什么,它总是使用相对 URL,而不是像我期望的那样为主机添加前缀,如果我在行Ember.$.ajax(hash);的ember_data.js中放置断点,我可以看到hash.url只是"/bars"。我在这里错过了什么?

不要忘记将适配器设置为存储,如下所示:

App.Adapter = DS.RESTAdapter.extend({
    host: 'http://www.yourdomain.com',
    namespace: 'url/path/to/app'
});
App.Store = DS.Store.extend({
    adapter: App.Adapter
});

希望对你有帮助

在将 ember-data 升级到 1.0.0-beta2 版本后,这个答案对我有用。在遵循指南的某个阶段,我最终得到了一个较旧的版本,从那时起就发生了重大变化。

您是否正在尝试从本地文件系统运行 Ember 应用程序? 文件:///。。。 要使远程 AJAX 调用正常工作,您需要使用某种本地服务器通过 http(或 https)访问您的应用程序。

我认为您必须设置一些数据模型,然后为此模型连接适配器。

App.BarAdapter = DS.RESTAdapter.extend({
    host:'http://myserver.com',
});
App.Bar = DS.Model.extend({
  author: DS.attr('string'),
  title: DS.attr('string'),
});