使用FormData发送文件时,AJAX POST请求无法在safari中工作

时间:2017-07-20 12:58:25

标签: javascript jquery ajax safari

我有一个API,可以在谷歌云服务器上传文件并返回网址。我正在使用jquery ajax进行API调用。我使用FormData()对象包含文件。它在chrome中运行良好,但是当我在safari中尝试相同时,我会收到错误,该错误表示无法发布/ my-own-site/link/index.html

这是我的代码

  <form method = "post" enctype = "multipart/form-data" id = "get-cloud-link">
     <input type="file" name = "filename" id = "image-input" accept=".jpg,.png,.jpeg"/>
     <input type="text" name = "tag" value = "iwh" class = "hidden"/>
     <input type="text" name = "empID" value = "" class = "hidden"/>
     <input class = "theme-btn" type="submit" id = "get-cloud-link-btn" value = "Upload"/>
  </form>


  $("form#get-cloud-link").submit(function(e){

       var imageInput = $("#image-input");

     if(imageInput.val() == '' || imageInput.val() == null){
       alert('No image is selected, please select image first');
      } else {
     var formData = new FormData($(this)[0]);

     for (var pair of formData.entries()) {
       console.log(pair[0]+ ', ' + pair[1]);
     }

      $.ajax({
         url : ' link-to-api/external/upload',
         type : "POST",
         cache : false,
         contentType : false,
         processData : false,
         headers : {
            "appID" : "",
            "version" : "",
            "empID" : ""
         },
         data : formData,
         beforeSend : function(){
         $(".image-uploading-wrapper").show();
         //xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
        },
         complete : function(){$(".image-uploading-wrapper").hide();},
         success : function(data){

         createUploadImage(data.imageUrl);

         imageInput.val("");

      },
         error : function(e){console.log(e)}
      });
    }
        e.preventDefault();
  });

0 个答案:

没有答案