win*_*yer 9 ember.js ember-data
我必须处理一个不受我控制的RESTful服务器.当我尝试从中获取ID 1记录时,这是我得到的错误:
XMLHttpRequest cannot load http://www.example.com/api/v1/companies/1.
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:4200' is therefore not allowed
access.
Run Code Online (Sandbox Code Playgroud)
我可以curl在shell上:
$ curl -I http://www.company.com/api/v1/companies/1
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 11055
Content-Type: application/javascript
Last-Modified: Thu, 18 Jun 2015 07:30:26 GMT
Accept-Ranges: bytes
ETag: "5e772a598a9d01:0"
P3P: policyref="/w3c/p3p.xml",CP="CAO DSP LAW CURa ADMa DEVa CUSi OUR LEG UNI"
Date: Fri, 19 Jun 2015 13:06:46 GMT
$
Run Code Online (Sandbox Code Playgroud)
我使用以下contentSecurityPolicy:
contentSecurityPolicy: {
'default-src': "'none'",
'script-src': "'self'",
'font-src': "'self'",
'connect-src': "'self' http://www.example.com",
'img-src': "'self'",
'style-src': "'self'",
'media-src': "'self'"
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?我如何告诉Ember使用它?
设置contentSecurityPolicy允许浏览器实际上是从发出请求http://localhost:4200到http://www.example.com.
如果你没有这个设置,你会看到如下错误:
[仅限报告]拒绝连接到" http://www.example.com/ ",因为它违反了以下内容安全策略指令:"connect-src'self'http :// localhost:*ws:// localhost:*ws:// localhost:35729 ws://0.0.0.0:35729".
执行请求后,如果http://www.example.com不包含实际允许http://localhost:4200发出这些请求的特定标头,则浏览器会抛出错误.
有关更多信息,请查看以下问题:Access-Control-Allow-Origin标头如何工作?
如果您使用Ember CLI进行开发,则可以将所有ajax请求代理为http://www.example.com/使用:
ember server --proxy http://www.example.com/
Run Code Online (Sandbox Code Playgroud)
但这并不能解决您转向生产时遇到的问题.你需要一些其他解决方案.
| 归档时间: |
|
| 查看次数: |
13759 次 |
| 最近记录: |