2

我正在尝试使用 Uploadcare 的 REST API 删除图像,我所做的与文档和这篇文章中所说的完全一样,但仍然无法删除图像,这是我的代码:

的HTML:

<html>
  <script charset="utf-8" src="https://ucarecdn.com/libs/widget/3.2.2/uploadcare.full.min.js"></script>
  <body>
    <div class="main">

    </div>
    <div>

      <button class="uploader">Upload an image</button>
      <input class="deletet" />
      <button class="getit">delete something</button>
    </div>
  </body>
  <footer>Developep by Francisco Jimenez</footer>
</html>   

JavaScript:

function deleteb(uuid){
 $.ajax({`
    url: "https://api.uploadcare.com/files/"+uuid+"/",`
    type: "DELETE",
    headers: { "Access-Control-Allow-Origin": "*",
              "Access-Control-Allow-Headers": "*",
              "Accept": "application/vnd.uploadcare-v0.5+json",
              "Access-Control-Allow-Methods": "HEAD, GET, OPTIONS",
             "Authorization": "Uploadcare.Simple publickey:privatekey"
            },
    success: function(result){
      alert("yessss");
      console.log(result);
    },
    error: function (result){
      alert("ouuuh");
      console.log(result);
    }
  });
}

我不断得到的响应是::Cross-Origin Request Blocked同源策略不允许在https://api.uploadcare.com/files/c2e166b5-17b9-493f-bf8c-b33da27842ca~1/读取远程资源。(原因:缺少 CORS 标头“Access-Control-Allow-Origin”)。

我做错了什么?

4

1 回答 1

2

您正在为请求设置响应 CORS 标头。这没有意义,但更重要的是服务器由于无法识别的标头而拒绝您的请求。例如,您的代码出现以下错误:

预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 Access-Control-Allow-Origin。

以下代码非常适合我:

$.ajax({
  url: "https://api.uploadcare.com/files/8b147fe2-b677-407b-8c28-3d596187ac93/",
  type: "DELETE",
  headers: {
    "Accept": "application/vnd.uploadcare-v0.5+json",
    "Authorization": "Uploadcare.Simple demopublickey:demoprivatekey"
  },
  success: function(result){
    alert("yessss");
    console.log(result);
  },
  error: function (result){
    alert("ouuuh");
    console.log(result);
  }
});
于 2018-02-19T15:55:39.733 回答