问题描述
我是本机反应的新手.我创建了一个表单来将图像上传到服务器.但是当我尝试上传图片时,总是出现这样的错误 =>[未处理的承诺拒绝:SyntaxError:JSON 解析错误:无法识别的标记 '<']请帮忙谢谢.我想以多部分格式发送数据
I am new to react native. I have created a form to upload images to server. But when I try to upload images keep getting error like this =>[Unhandled promise rejection: SyntaxError: JSON Parse error: Unrecognized token '<']please help thanks. I want to send data in multipart format
这是我的代码
const pickImage = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
// base64: true,
});
if (result.cancelled) {
return;
}
let localUri = result.uri;
let filename = localUri.split('/').pop();
let match = /\.(\w+)$/.exec(filename);
let type = match ? `image/${match[1]}` : `image`;
let formData = new FormData();
formData.append('photo', { uri: localUri, name: filename, type });
console.log(formData)
return await fetch('https//xyxtech/Android_API_CI/upload_multipart_data', {
method: 'POST',
body: formData,
// header: {
// 'content-type': 'multipart/form-data',
// },
headers: {'Accept': 'application/json, text/plain, */*', 'content-type': 'multipart/form-data'},
})
.then((returnValue) => returnValue.json())
// .catch(err=>err)
.then(function(response) {
console.log(response)
Alert.alert("File uploaded");
// return response.json();
});
};
推荐答案
这本身不是 React Native 问题.您的服务器使用无法在此处解析的非 JSON 正文进行响应:
This is not a React Native issue per se. Your server responds with non-JSON body which cannot be parsed here:
.then((returnValue) => returnValue.json())
这可能意味着您的 HTTP 请求不正确.为什么不正确 - 抱歉,无法帮助您,因为我不熟悉您尝试使用的 API.
This likely means that your HTTP request is incorrect. Why is it incorrect – sorry, can't help you, since I'm not familiar with the API you are trying to use.
这篇关于未处理的承诺拒绝:SyntaxError:JSON 解析错误:无法识别的令牌“<"在反应原生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!