如何在EmberJS应用程序中启用CORS? [英] How to enable CORS in an EmberJS application?

查看:209
本文介绍了如何在EmberJS应用程序中启用CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EmberJS应用程序使用Ember数据通过REST API访问数据。 REST API在同一台计算机上运行,​​但在不同的端口上运行(尽管这可能适用于从另一个域提供的REST API)。

I have an EmberJS application that uses ember-data to access data via a REST API. The REST API is running on the same machine but on a different port (although this probably applies to REST API's that are served from another domain.

当我转到URL localhost:4200 / items 我在Firefox控制台中收到以下错误:

When I go to the URL localhost:4200/items I get the following error in the Firefox console:


内容安全策略:网页的设置在 http:// localhost:7654 / api / items (connect)阻止了资源的加载-src http:// localhost:4200 ws:// localhost:35729 ws://0.0.0.0:35729 http://0.0.0.0:4200 )。

Content Security Policy: The page's settings blocked the loading of a resource at http://localhost:7654/api/items ("connect-src http://localhost:4200 ws://localhost:35729 ws://0.0.0.0:35729 http://0.0.0.0:4200").

我试过安装 ember-cli-cors 但没有改变,我也尝试了 http://discuss.emberjs.com/t/ember-data-and-cors/3690 ,但这也没有工作。讨论是从2013年,所以不是一个巨大的惊喜。

I tried installing ember-cli-cors but nothing changed. I also tried the solution at http://discuss.emberjs.com/t/ember-data-and-cors/3690, but that didn't work either. That discussion was from 2013, so that's not a huge surprise.

REST API是使用Flask和Flask-cors在Python中编写的。使用网络选项卡,我可以看到请求正在发送,数据正在发送回,但错误仍然存​​在。响应中的标头 Access-Control-Allow-Origin 设置为 http:// localhost:4200 预期。

The REST API is written in python using Flask and Flask-cors. Using the network tab I can see that the request is being sent, and the data being sent back, but the error is still there. The header Access-Control-Allow-Origin is set to http://localhost:4200 in the response, as expected.

app / router.js

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

export default Router.map(function() {
  this.route('items');
});

app / adapters / application.js

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  namespace: 'api',
  host: 'http://localhost:7654',
});

app / routes / items.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return this.store.find('item');
  }
});

app / models / item.js

import DS from 'ember-data';

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

app / templates / items.hbs b

{{#each item in items}}
  {{ item.name }}<br>
{{else}}
  <p>No items</p>
{{/each}}

{{outlet}}


推荐答案

这是一个CSP问题,不是CORS

This is a CSP issue not CORS

在config / environment.js里面找到ENV.contentSecurityPolicy并添加< =http:// localhost:7654 =nofollow> http:// localhost:7654 到您的connect-src键

Inside config/environment.js find the ENV.contentSecurityPolicy and add http://localhost:7654 to your 'connect-src' key

例如

ENV.contentSecurityPolicy = {
  // ... other stuff here
  'connect-src': "'self' http://localhost:7654"
}

可能需要为您的生产环境设置不同的设置。

You will probably need a different setting for your production environment as well.

这篇关于如何在EmberJS应用程序中启用CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆