Giphy上传工作与.ajax,但不读取



我想上传一个gif到Giphy。它使用jQuery/。但是我不能让它与fetch工作。

.ajax版本:

$.ajax({
type: "POST",
url: "http://upload.giphy.com/v1/gifs",
data: {
api_key: "E0sxxxxxxxxxxxxxxxxxxxxxxxxxx",
source_image_url: "https://urlto/snoopy.gif",
tags: "cute dog",
},
success: function (data) {
console.log(data);
},
error: function (data) {
alert("fail");
},
});
and here is the fetch version which returns a 401 error:
fetch("https://upload.giphy.com/v1/gifs", {
method: "POST",
body: {
api_key: "E0sxxxxxxxxxxxxxxxxxxxxxxxxxx",
source_image_url: "https://urlto/snoopy.gif",
tags: "cute dog",
},
}).then((response) => {
console.log("status:");
console.log(response);
});

我没有学过fetch,所以任何帮助我做错了什么将是伟大的。

您不能像在$.ajax中那样将普通对象作为请求主体来传递获取。使用URLSearchParams对象

fetch("https://upload.giphy.com/v1/gifs", {
method: "POST",
body: new URLSearchParams({
api_key: "E0sxxxxxxxxxxxxxxxxxxxxxxxxxx",
source_image_url: "https://urlto/snoopy.gif",
tags: "cute dog"
})    
}).then((response) => {
console.log("status:");
console.log(response);
});

最新更新