AngularJS发送OPTIONS请求而不是POST

前端之家收集整理的这篇文章主要介绍了AngularJS发送OPTIONS请求而不是POST前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将图片上传到我的S3桶.我使用AngularJS v1.2.13.
当我使用他们的文档中显示的简单情况(提交表单与动作标签)一切正常.但是,如果我想这样做,Angular的方式用ng-click Angular发送OPTIONS请求而不是POST请求.

以下是服务代码,它首先去服务器获取签名(我知道那部分是好的)然后尝试POST一切.

  1. myServices.factory('s3',function($http) {
  2. var service = {};
  3.  
  4. service.upload = function(fileName) {
  5.  
  6. return $http(
  7. {
  8. method:"POST",url: "sign",data: { "fileName": fileName }
  9. }
  10. ).then(
  11. function(result) {
  12. // success
  13. //resolve the promise as the data
  14. var data = result.data;
  15. var url = "https://" + data.bucket + ".s3.amazonaws.com/";
  16.  
  17. return $http.post(url,{
  18. "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"
  19. }).then(
  20. function(response) {
  21. // success
  22. console.log("s3.upload,success: ");
  23. console.log(response);
  24. },function(response) {
  25. // Failed
  26. console.log("s3.Upload,fail: ");
  27. console.log(response);
  28. }
  29. );
  30.  
  31. },function(response) {
  32. // Failed
  33. console.log("s3.sign,fail: ");
  34. console.log(response);
  35. }
  36. );
  37. };
  38.  
  39. return service;
  40. });

我究竟做错了什么?

我不知道这些资源是否会帮助你,但这听起来像是同样的问题:

HTTP POST turns into OPTIONS

OPTIONS preflight call preceding a $http.post request

猜你在找的Angularjs相关文章