TypeError: Failed to execute 'fetch' on 'Window': Invalid value

后端 未结 8 631
情歌与酒
情歌与酒 2020-12-24 14:24

I\'ve tried to use fetch to call from backend using react, without libs (such as Axios). So I created this function:

export function api(url, method, body, i         


        
8条回答
  •  醉酒成梦
    2020-12-24 15:23

    This error happened to me as well, however with different circumstances. I would like to share it in case someone else has this issue.

    So I got the error "TypeError: Failed to execute 'fetch' on 'Window': Invalid value" when I tried to upload a file with HTTP POST without using FormData. To solve the issue I used FormData object and appended properties and file itself:

    let formData = new FormData();
    formData.append('name', fileName);
    formData.append('data', file);
    

    then I used fetch to send the file with POST method. Here is pseudo code:

    const options = {method: 'POST', formData};
    fetch('http://www.someURL.com/upload', options);
    

提交回复
热议问题