本文介绍了向API请求formData,在上传图片时在axios中获得“网络错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向服务器发出POST请求,以上传图像并使用react-native中的axios发送formdata.我收到网络错误".我也尝试获取,但没有任何效果.使用React Native Image Picker libeary选择图片.在邮递员api中工作正常

I am making a POST request to server to upload an image and sending formdata using axios in react-native. i am getting "Network Error". i also try fetch but nothing work.using react native image picker libeary for select image.in postman api working fine

        formData.append('title', Title);
        formData.append('class_id', selectClass._id)
        formData.append('subject_id', checkSelected)
        formData.append('teacher_id', userId)
        formData.append('description', lecture);
        formData.append('type', 'image');

       var arr=[];
       arr.push(imageSource)
       arr.map((file,index)=>{
       formData.append('file',{
       uri:file.path,
       type:file.type,
       name:file.name
       })
       })


       axios({
       method: 'post',
       url: URL + 'admin/assignment/create',
       data: data,
       headers: {
       "content-type": "multipart/form-data",
       'x-auth-token': token,
        },
       })
     .then(function (response) {
    //handle success
    console.log('axios assigment post',response);
      })
   .catch(function (response) {
     //handle error
      console.log('axios assigment post',response);
    });

推荐答案

"react-native":"0.62.1","react":"16.11.0","axios":"^ 0.19.2",

"react-native": "0.62.1","react": "16.11.0","axios": "^0.19.2",

奇怪的解决方案,我必须删除调试文件夹在android-> app-> source-> debug

weird solution i have to delete debug folderin android ->app->source->debug

,然后重新启动应用程序它解决了我的问题.我认为这是缓存问题.

and restart the app againits solve my problem. i think it's cache problem.

这篇关于向API请求formData,在上传图片时在axios中获得“网络错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:03