已被CORS政策阻止:对预检请求的响应未通过访问控制检查

时间:2019-07-13 16:55:57

标签: javascript html api

大家好,希望您周末愉快!我试图弄清楚为什么这种情况会在不熟悉Javascript的情况下发生,但我确实了解它的工作原理。

我想做的是通过javascript将文件上传到Seafile API,但出现以下错误

从原点“ example.com”对“ https://example.com/seafhttp/upload-api/cf3665cc-fcd8-4260-9686-94b7c40702e3”处XMLHttpRequest的访问已被CORS策略阻止:对预检请求的响应未通过访问控制检查:无“ Access-Control-Allow-Origin”标头出现在请求的资源上。

                            <form enctype="multipart/form-data">
                                     Select file to upload:
                                <input type="file" name="fileToUpload" id="fileToUpload">

                                <progress></progress>
                            </form>

                             <script>

$(':file').on('change', function () {
  $.ajax({
    // Your server script to process the upload
    url: 'https://example.com/seafhttp/upload-api/cf3665cc-fcd8-4260-9686-94b7c40702e3',
    type: 'POST',

    // Form data
    data: new FormData($('form')[0]),

    // Tell jQuery not to process data or worry about content-type
    // You *must* include these options!
    cache: false,
    contentType: false,
    processData: false,

    // Custom XMLHttpRequest
    xhr: function () {
      var myXhr = $.ajaxSettings.xhr();
      if (myXhr.upload) {
        // For handling the progress of the upload
        myXhr.upload.addEventListener('progress', function (e) {
          if (e.lengthComputable) {
            $('progress').attr({
              value: e.loaded,
              max: e.total,
            });
          }
        }, false);
      }
      return myXhr;
    }
  });
});
</script>

0 个答案:

没有答案