2014-03-12 207 views
12

我試圖將圖片上傳到我的S3存儲桶。我正在使用AngularJS v1.2.13。 當我使用他們的文檔中顯示的簡單案例(使用action標籤提交表單)時,一切正常。但是,如果我想要這樣做,那麼Angular將發送OPTIONS請求,而不是請求POSTAngularJS發送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; 
}); 

我在做什麼錯?

回答

-1

在CORS S3頭添加標題:

Access-Control-Allow-Origin:*

相關問題