首页 > 解决方案 > react-native 使用emu成功发送图像表单数据,但在真实设备上出现错误422

问题描述

所以这是我的问题:我的代码在从真实设备发送数据时只返回错误 422,但在模拟器上做得很好是我的表单数据代码:

const data = new FormData();
data.append('approval_store_code', this.state.selectedApotik.store_code);
data.append('delivery_method_id', 1);
data.append('recipe_document_images[]', {
  name: 'recipePhotos1',
  type: 'image/jpeg',
  uri:
    Platform.OS === 'android'
      ? this.state.imageResep[0].uri
      : this.state.imageResep[0].uri.replace('file://', ''),
});

这是我发送到服务器时的代码

      this.setState({spinner: true});
      const response = await REQ.createRecipe(this.state.accessToken, data);
      console.log(response);
      if (response.status == 200) {
        console.log(response);
        this.setState({spinner: false});
        this.props.navigation.navigate('SuccessFormPharmacy');
      } else {
        alert('error');
        this.setState({spinner: false});
      }
    } catch (error) {
      this.setState({spinner: false});
      console.log('error send data : ', error);
      alert('error send Data!');
      return null;
    }

axios 代码:

return axios({
    method: 'POST',
    url: `${API.BASE_URL}/v1/pharmacy/order`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      Authorization: `Bearer ${accessToken}`,
    },
    body: formData,
  });

奇怪的是,我尝试使用 fetch 而不是 axios,不适用于真实设备,但在模拟器上做得很好我尝试先将图像保存到设备,它保存但没有成功发送到服务器

在真实设备上返回错误 422

标签: androidiosreact-nativeaxioshttp-status-code-422

解决方案


推荐阅读