使用ember.js时使用Access-Control-Allow-Origin错误(使用ember-cli)

Chi*_*gDi 6 javascript http ember.js

这是我在(app/routes/customers.js)的路线:

export default Ember.Route.extend({
  model: function() {
    return $.getJSON("http://127.0.0.1:3000/odata/customers");
  }
});
Run Code Online (Sandbox Code Playgroud)

这是我的router.js:

export default Router.map(function() {
  this.route('customers', {path: '/'});
});
Run Code Online (Sandbox Code Playgroud)

http://127.0.0.1:3000/odata/customers是我的api,http://localhost:4200/但当我打开时http://localhost:4200/,使用ember-cli ,

在控制台中,错误消息是: XMLHttpRequest cannot load http://127.0.0.1:3000/odata/customers. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

我找到一篇文章:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

所以我知道什么是错的,但我不知道在使用ember.js时如何修复它.

抱歉我的英语不好,希望这清楚......

Jim*_*dra 7

这不是Ember问题.它是端口3000上的服务器.如果您的Ember应用程序在不同的端口上运行,它基本上是跨域的,因此端口3000上的服务器必须启用CORS.对于节点js和express,这样的示例是这样的:http://enable-cors.org/server_expressjs.html 您需要弄清楚如何为后端执行此操作.但它归结为基本上只是将正确的标题添加到响应流中.

其他一些例子:

http://enable-cors.org/server_iis7.html

http://enable-cors.org/server_php.html

  • 这个答案似乎不正确。显然,加载的第一个页面是由 ember cli 应用程序提供的,该应用程序在端口 4200 上运行。加载第一个页面的人负责设置 Access-Control-Allow-Origin 标头。端口 3000 上的 API 服务器与此无关。相反,ember 服务器应该告诉浏览器可以对 localhost:3000 进行 ajax 调用,这是通过我提到的标头完成的。 (2认同)