我的本地局域网(machineA)上有一台有两台Web服务器的机器.第一个是XBMC中的内置版(在端口8080上)并显示我们的库.第二个服务器是CherryPy python脚本(端口8081),我用它来按需触发文件转换.文件转换由来自XBMC服务器提供的页面的AJAX POST请求触发.
jQuery Ajax请求
$.post('http://machineA:8081', {file_url: 'asfd'}, function(d){console.log(d)})
Run Code Online (Sandbox Code Playgroud)
请求标题 - 选项
Host: machineA:8081
User-Agent: ... Firefox/4.01
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://machineA:8080
Access-Control-Request-Method: POST
Access-Control-Request-Headers: x-requested-with
Run Code Online (Sandbox Code Playgroud)
响应标题 - 选项(状态= 200 OK)
Content-Length: 0
Access-Control-Allow-Headers: *
Access-Control-Max-Age: 1728000
Server: CherryPy/3.2.0
Date: Thu, 21 Apr 2011 22:40:29 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Content-Type: text/html;charset=ISO-8859-1
Run Code Online (Sandbox Code Playgroud)
为了排除故障,我还从http://jquery.com发出了相同的$ .post命令.这是我难倒的地方,从jquery.com开始,post请求有效,OPTIONS请求由POST发送.此交易的标题如下;
请求标题 - …
我有一个angular.js应用程序,我需要做CORS请求.
我想使用角度资源定义我的休息服务"angular",如下所述:http://docs.angularjs.org/tutorial/step_11.
但我还没有找到办法让这个工作.在谷歌上我发现了以下示例代码:http://jsfiddle.net/ricardohbin/E3YEt/,但这似乎不适用于角度资源.
这是我的app.js
'use strict';
angular.module('corsClientAngularApp', ['helloServices'])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
});
});
Run Code Online (Sandbox Code Playgroud)
这是我的services.js与其他服务
angular.module('helloServices', ['ngResource']).
factory('Hello', function($resource){
return $resource('http://localhost:8080/cors-server/hello/:name', {}, {
query: {method:'GET', params:{name:'name'}, isArray:false}
});
});
Run Code Online (Sandbox Code Playgroud)
这是我的main.js与控制器使用$ http,这是有效的!:
'use strict';
angular.module('corsClientAngularApp')
.controller('MainCtrl', function ($scope, $http, Hello) {
$http.defaults.useXDomain = true;
$http.get('http://localhost:8080/cors-server/hello/stijn')
.success(function(data) {
$scope.hello = data;
});
});
Run Code Online (Sandbox Code Playgroud)
这是我使用角度资源的main.js的另一个版本.这不起作用:(
'use strict';
angular.module('corsClientAngularApp')
.controller('MainCtrl', function ($scope, $http, Hello) {
$http.defaults.useXDomain …Run Code Online (Sandbox Code Playgroud) 最近我启用了Amazon S3 + CloudFront作为我的rails应用程序的CDN.为了使用字体资源并在Firefox或IE中显示它们,我必须在我的S3存储桶上启用CORS.
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
Run Code Online (Sandbox Code Playgroud)
然后我用了curl -I https://small-read-staging-assets.s3.amazonaws.com/staging/assets/settings_settings-312b7230872a71a534812e770ec299bb.js.gz,我得到了:
HTTP/1.1 200 OK
x-amz-id-2: Ovs0D578kzW1J72ej0duCi17lnw+wZryGeTw722V2XOteXOC4RoThU8t+NcXksCb
x-amz-request-id: 52E934392E32679A
Date: Tue, 04 Jun 2013 02:34:50 GMT
Cache-Control: public, max-age=31557600
Content-Encoding: gzip
Expires: Wed, 04 Jun 2014 08:16:26 GMT
Last-Modified: Tue, 04 Jun 2013 02:16:26 GMT
ETag: "723791e0c993b691c442970e9718d001"
Accept-Ranges: bytes
Content-Type: text/javascript
Content-Length: 39140
Server: AmazonS3
Run Code Online (Sandbox Code Playgroud)
我应该看看'Access-Control-Allow-Origin'哪里?S3是否需要时间来更新CORS设置?如果缓存它们,我可以强制过期标头吗?
amazon-s3 font-face cors amazon-cloudfront ruby-on-rails-3.2