向 API 请求 formData,上传图片时 axios 出现“网络错误" [英] request formData to API, gets “Network Error” in axios while uploading image

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

问题描述

我正在向服务器发出 POST 请求以上传图像并在 react-native 中使用 axios 发送表单数据.我收到网络错误".我也尝试 fetch 但没有任何效果.使用 react native image picker libeary 选择 image.in postman 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",反应":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 folder in android ->app->source->debug

并再次重新启动应用程序它解决了我的问题.我认为是缓存问题.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆