简体   繁体   English

React Native Fetch 给出错误“网络请求失败”

[英]React Native Fetch give error "Network request failed"

I have the following code for creating an event with a image and some body params.我有以下代码用于创建带有图像和一些正文参数的事件。 It was working fine when i was doing it without image, i am using react-native-image-crop-picker for selecting images.当我没有图像时它工作正常,我使用react-native-image-crop-picker来选择图像。 I am getting "Network request failed" error when posting data from react-native.从 react-native 发布数据时出现“网络请求失败”错误。 The request never reach my backend as i am getting no logs there.该请求永远不会到达我的后端,因为我在那里没有收到任何日志。 It is working fine with postmen.它与邮递员一起工作得很好。

MY CODE:我的代码:

const { name, date, description, location, uri, mime, time } = this.state;

    const formData = new FormData();
    formData.append('name', name)
    formData.append('date', date)
    formData.append('description', description)
    formData.append('location', location)
    formData.append('time', time)

    formData.append('image',{
      uri:uri,
      mime:'image/jpeg',
      name:`image${moment()}`
    })


    alert(JSON.stringify(formData));
    const config = {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'multipart/form-data',
          },
          body: formData,
      };

    fetch(`http://${Config.apihost}:${Config.port}/events`,config).then((res) => res.json())
      .then((res) => {
        this.setState({ modalVisible: false, name:'', date: moment().format('YYYY-MM-DD'), description:'', Location: 'AlHedaya Masjid' })
        this.props.addEvent(res.message);

      // this.props.navigation.goBack();
      }).catch((err) => alert(err));

I have another screen which contains different number of pictures like gallery i am uploading multiple picture to the gallery, the request is working fine with code below.我有另一个屏幕,其中包含不同数量的图片,例如图库,我正在将多张图片上传到图库,请求在下面的代码中工作正常。

 const data = new FormData();
        data.append('name', 'avatar');
        images.map((res, i) => {
          data.append('fileData[]', {
            uri: res.path,
            type: res.mime,
            name: `image${i}${moment()}`
          });
        })
        const config = {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'multipart/form-data',
          },
          body: data,
        };
        fetch(`http://${Config.apihost}:${Config.port}/events/${this.state.item.id}/photos`, config)
          .then((checkStatusAndGetJSONResponse) => checkStatusAndGetJSONResponse.json())
          .then((response) => {

            if (response.status && response.message.length > 0) {
              var images = this.state.images;
              response.message.map(file => {
                images.push(`http:${Config.apihost}:${Config.port}/images/${file.id}`);
              });
              this.setState({ images });
            }
          }).catch((err) => { alert(err) });

I can't really see the difference between the two codes but the upper code giving me error.我真的看不出这两个代码之间的区别,但是上面的代码给了我错误。

Am I missing something?我错过了什么吗?

In first code snippet you have written mime instead of type .在第一个代码片段中,您编写了mime而不是type

formData.append('image',{
      uri:uri,
      **mime:'image/jpeg**',
      name:`image${moment()}`
    })

it should be like below snippet它应该像下面的片段

formData.append('image',{
          uri:uri,
          type:'image/jpeg',
          name:`image${moment()}`
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM