我是否需要配置localhost cors?

时间:2014-07-10 14:29:05

标签: ruby-on-rails-3 amazon-s3 cors jquery-fileupload-rails

我已整合carrierwave_direct直接上传到我的s3广告位。我是通过jquery-fileupload接管的模态弹出框执行此操作的。当我没有使用jquery时,这一切都很完美,但是现在它上传了文件,但在jquery-fileupload中触发了fail方法。

我的存储桶具有以下角色配置:

<?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>

控制器:

def file_upload
  @uploader = StudentTranscript.new.upload
  @uploader.success_action_redirect = new_student_student_transcript_url #NEVER GETS CALLED
end

我的js如下:

$('#new_file_uploader').fileupload({
  dataType: "script",
  add: function(e, data) {
    var file, types;
    types = /(\.|\/)(gif|jpe?g|png|zip)$/i;
    file = data.files[0];

    if (types.test(file.type) || types.test(file.name)) {
      data.context = $(tmpl("template-upload", file));
      $('#new_file_uploader').append(data.context);
      data.submit();
    } else {
      alert("" + file.name + " is not a gif, jpeg, or png image file");
    }
  },
  progress: function(e, data) {
    var progress;
    if (data.context) {
      progress = Math.round((data.loaded / data.total) * 100) || 0;
      data.context.find('.bar').css('width', progress + '%');
    }
  },
  done:function (e, data) {
    alert("Success");
  },
  fail: function(e, data) {
    alert('Fail!');
  }
});

当我上传文件时,一切正常,即文件上传但是当s3将响应发送回localhost时,我得到以下内容:

XMLHttpRequest cannot load https://bucket.s3.amazonaws.com/. The request was redirected to 'http://localhost:3000/student/student_transcripts/new?bucket=antdna&key=upl…hot+2014-06-24+at+18.13.53.png&etag=%221d6d718f8468e3044ab0a61bde5c3bcc%22', which is disallowed for cross-origin requests that require preflight.

我是否需要在我的Rails应用程序上设置cors配置?我看过一些posts,人们在application_controller.rb中使用before_filters来设置标题

0 个答案:

没有答案