React Native:无法使用axios或fetch发出对formData的后期请求

时间:2020-01-13 15:47:15

标签: react-native axios fetch

我一直在尝试通过axios / fetch发送请求以发送formData,但是每次我提出请求时,它都会引发“网络错误”。我已经尝试了几天了,但是做不到。

    var file = new File([this.state.selectedFileObj], "ISDD_" + this.state.fileName, { lastModified: new Date().getMilliseconds() })

    formdata.append("file", file, this.state.fileName);
    formdata.append("folderName", this.state.folderName);
    formdata.append("userName", "user@domain.com");
    formdata.append("documents", documents);

// 1st approach
  RNFetchBlob.fetch('POST', url, {
        "Content-Type": 'multipart/form-data',
        "enctype": 'multipart/form-data',
        "Cache-Control": 'sno-cache',
        "Pragma": 'no-cache'
    }, formdata)
        .then((response) => console.log(`1 ${response.text()}`))
        .then((RetrivedData) => {
            console.log(`2 ${RetrivedData}`);
        })
        .catch((err) => {
            console.log(`err ${err}`);
        })

 //2nd approach
 axios({
        url: url, formdata,
        method: 'POST',
        headers: {
            "Content-Type": 'multipart/form-data',
            'enctype': 'multipart/form-data',
            'Cache-Control': 'sno-cache',
            'Pragma': 'no-cache'
        },
        data: formdata
    })
        .then((response) => {
            console.log(`1 ${response}`)
        })
        .catch((error) => {
            console.log(error)
        })

需要解决方案, 谢谢,

0 个答案:

没有答案