我尝试使用Uploadcare的REST API删除图片,我完全按照文档和post说的那样做,但仍然无法删除image,这是我的代码:
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'。
我做错了什么?
答案 0 :(得分:2)
您正在为请求设置响应CORS标头。这没有任何意义,但更重要的是服务器因无法识别的标头而拒绝您的请求。例如,我的代码出现以下错误:
请求标题字段Access-Control-Allow-Origin在预检响应中不允许使用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);
}
});