我最近在开发的应用程序的iOS版本遇到问题。通常,在向服务器发送图像(使用组件expo-image-picker选择)时,它会返回错误:
Error procession request body: Error Domain=ABI34_0_0RCTErrorDomain Code=0
"Invalid request token"
UserInfo={NSLocalizedDescription=Invalid request token.}
这是发送数据的方法:
alterUserDataOnDatabase(formValues, callback) {
fetch(url , {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
},
body: formValues
})
.then(response => response.json())
.then(responseJson => callback(responseJson));
}
这是使用图像选择器获取图像本身的代码
_pickImage = async () => {
const {
status: cameraRollPerm
} = await ImagePicker.getCameraRollPermissionsAsync();
//await Permissions.getAsync(Permissions.CAMERA_ROLL);
// only if user allows permission to camera roll
if (cameraRollPerm === 'granted') {
try {
let pickerResult = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 4],
});
this._handleImagePicked(pickerResult);
} catch (e) {
console.log(e)
}
}
};
_handleImagePicked = async pickerResult => {
try {
this.setState({
uploading: true
});
if (!pickerResult.cancelled) {
let uri = pickerResult.uri;
let uriParts = uri.split('.');
let fileType = uriParts[uriParts.length - 1];
this.form.append('photo', {
uri: uri,
name: `photo.${fileType}`,
type: `image/${fileType}`,
});
this.setState({
image: uri
});
}
} catch (e) {
console.log({ e });
alert('Upload failed, sorry :(');
} finally {
this.setState({
uploading: false
});
}
};
我在34版本中使用了expo和react-native,我曾尝试将它们转换为36版本,但它并没有为此做任何事情,错误仅从ABI34更改为ABI36。
错误仅在某些时候发生,这是一个奇怪的部分,它似乎是随机发生的,但是通常,在发送一个图像后,下一个图像将返回错误,尽管有时是在我第一次尝试发送时发生的。
是否有人对此错误有任何了解,我很难找到答案。
编辑:我尝试在Expo上创建新的空应用程序,代码和程序包可在新应用程序上使用,但似乎无法与所有内容一起使用。
我已经找到了解决方法。
在将数据发送到服务器之前,我曾经this.setState()
在应用中启动动画,因此用户在发送数据后无法按下按钮(为此,我正在使用组件'react-native-loading-spinner -覆盖')。
由于某种原因(我无法辨别),更新框架中的状态是因为发送带有图像的表格会导致此错误。要解决此问题,我只是将方法用于将数据发送到上的服务器,setTimeout
并使其在发送之前等待100毫秒。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句