这是我在 (app/routes/customers.js) 中的路线:
export default Ember.Route.extend({
model: function() {
return $.getJSON("http://127.0.0.1:3000/odata/customers");
}
});
这是我的 router.js:
export default Router.map(function() {
this.route('customers', {path: '/'});
});
http://127.0.0.1:3000/odata/customers
是我的 api,但是 ember-cli 使用http://localhost:4200/
,当我打开http://localhost:4200/
,
在控制台中,错误消息是:
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 https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
所以我知道出了什么问题,但我不知道在使用 ember.js 时如何修复它。
抱歉我的英语不好,希望这是清楚的......
问题不在于 Ember。它是端口 3000 的服务器。如果您的 Ember 应用程序运行在不同的端口上,那么它基本上是跨域的,因此端口 3000 的服务器必须启用 CORS。 Node js 和 Express 的示例如下:http://enable-cors.org/server_expressjs.html http://enable-cors.org/server_expressjs.html您需要弄清楚如何为后端执行此操作。但归根结底只是将正确的标头添加到响应流中。
其他一些例子:
http://enable-cors.org/server_iis7.html http://enable-cors.org/server_iis7.html
http://enable-cors.org/server_php.html http://enable-cors.org/server_php.html
请注意,如果您确实想阻止 Ember 服务器向其他来源发送请求,您可以使用内容安全政策 https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)