AngularJS发送OPTIONS请求而不是POST

McM*_*eep 15 post amazon-s3 httprequest angularjs

我正在尝试将图片上传到我的S3存储桶.我正在使用AngularJS v1.2.13.当我使用他们的文档中显示的简单案例(提交带action标签的表单)时,一切正常.但是,如果我想这样做,Angular的Angular方式会ng-click发送OPTIONS请求而不是POST请求.

以下是服务代码,它首先到服务器获取签名(我知道该部分没问题)然后尝试POST所有内容.

myServices.factory('s3', function($http) {
    var service = {};

    service.upload = function(fileName) {

        return $http(
            {
                method:"POST",
                url: "sign",
                data: { "fileName": fileName }
            }
        ).then(
            function(result) {
                // success
                //resolve the promise as the data
                var data = result.data;
                var url = "https://" + data.bucket + ".s3.amazonaws.com/";

                return $http.post(url, {
                    "key": data.key,
                    "AWSAccessKeyId": data.awsKey,
                    "acl": data.acl,
                    "policy": data.policy,
                    "signature": data.signature,
                    "Content-Type": "image/jpeg",
                    "success_action_redirect": "http://localhost:3000/s3Uploaded"
            }).then(
            function(response) {
                // success
                console.log("s3.upload, success: ");
                console.log(response);
            },
            function(response) { 
                // failed
                console.log("s3.Upload, fail: ");
                console.log(response);
            }
        );

    },
        function(response) { 
            // failed
            console.log("s3.sign, fail: ");
            console.log(response);
        }
    );
};

return service;
});
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

小智 0

在 CORS 的 S3 标头中添加标头:

访问控制允许来源:*