【发布时间】:2021-09-24 23:29:04
【问题描述】:
所以我试图通过 React Native 和 fetch API 将图像发布到服务器。
fetch(`${API}/uploadAvatar`, {
method: "POST",
headers: {
Authorization: bearer,
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/json",
},
body: JSON.stringify({ file: result.uri }),
})
.then((response) => response.json())
.then((json) => {
console.log({ json });
// this console.log outputs:
// "The format of the file should be jpg, png, jpeg.",
})
.catch((err) => {
console.log({ err });
});
}
result 返回:
{
"cancelled": false,
"height": 1776,
"type": "image",
"uri": "file:///var/mobile/Containers/Data/Application/18F84F29-CB72-4615-A68F-A00422D9B119/Library/Caches/ExponentExperienceData/%2540heythere%252Fkeep-up/ImagePicker/959E8BDE-FCF4-40C6-AF18-8F9EA852760D.jpg",
"width": 1776,
}
这些是 POSTMAN 上的调用,您可以在其中看到它们的工作原理。
我做错了什么?
【问题讨论】:
-
真正有帮助的是,如果您去 Postman 并单击“Cookie”旁边和“保存”按钮下方的“代码”。选择 JavaScript Fetch 并查看 sn-p 是否有您的代码没有的内容。
标签: javascript reactjs react-native ecmascript-6 fetch