简体   繁体   English

使用jQuery Ajax请求上传CORS Amazon S3文件

[英]CORS Amazon S3 file upload with jQuery Ajax request

I have scanned almost everything now and most people they're solution was simply to configure CORS on the S3 service, which doesn't do the trick for me. 我现在几乎扫描了所有内容,他们解决的大多数人只是在S3服务上配置CORS,这对我来说不起作用。 I must be missing something. 我肯定错过了什么。 Here it goes: 在这里:

I'm trying to upload my files to Amazon S3 using an Ajax call on the client-side. 我正在尝试使用客户端的Ajax调用将我的文件上传到Amazon S3。 I know my policy/signature are correct, since it works when I simply submit the form, but when I try and do an Ajax call with it I get the 我知道我的策略/签名是正确的,因为它只是在我提交表单时有效,但当我尝试用它进行Ajax调用时,我得到了

Origin "my host" is not allowed by Access-Control-Allow-Origin. 

Error. 错误。 My form: 我的表格:

<form id="fileform" action="https://mybucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
    <input type="hidden" name="key" value="mykey">
    <input type="hidden" name="AWSAccessKeyId" value="myaccesskey">
    <input type="hidden" name="acl" value="public-read">
    <input type="hidden" name="Content-Type" value="image/jpeg">
    <input type="hidden" name="policy" value="mypolicy">
    <input type="hidden" name="signature" value="mysignature">
  </form>

And in my CORS of the bucket I practically allow everything, because I'm desperate: 在我的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>HEAD</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

Choosing a file and submitting the form (either by clicking it or using jQuery) works like a charm, but performing an Ajax request with the serialized form gives me the error. 选择一个文件并提交表单(通过点击它或使用jQuery)就像一个魅力,但是使用序列化表单执行Ajax请求会给我带来错误。

var form = $('#fileform');
$.ajax({
  url: "https://mybucket.s3.amazonaws.com/",
  type: 'post',
  crossDomain: true,
  dataType: 'xml',
  data: form.serialize()
});

I know this has something to do with the CORS rules, but as you can see they are set up. 我知道这与CORS规则有关,但正如你所看到的那样,它们已经建立起来了。 Therefore, anyone any idea what else might be wrong? 因此,任何人都知道还有什么可能是错的?

Thanks. 谢谢。

You can add more HTML tags <input type="file" name="uploadFile" /> because without file you can not post the file data using serialize() method. 您可以添加更多HTML标记<input type="file" name="uploadFile" />因为没有文件您无法使用serialize()方法发布文件数据。 And I suggest you use serializeArray() instead of form.serialize() . 我建议你使用serializeArray()而不是form.serialize()

Turns out that the error message displayed in the Chrome console had nothing to do with the actual error. 事实证明,Chrome控制台中显示的错误消息与实际错误无关。

The problem was the jquery.serialize() does not save the file input because it is not supported in XMLHttpRequest (only in XMLHttpRequest 2). 问题是jquery.serialize()不保存文件输入,因为XMLHttpRequest不支持它(仅在XMLHttpRequest 2中)。 So the ajax request I was sending to S3 did not contain a file. 所以我发送给S3的ajax请求不包含文件。 Not sure why S3 was returning a CORS error, but once I switching to using FormData everything worked. 不确定为什么S3会返回一个CORS错误,但是一旦我切换到使用FormData,一切正常。 Now I just have to figure out how to support IE 8 & 9... ! 现在我只需要弄清楚如何支持IE 8和9 ......!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM