Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取API返回415(不支持的媒体类型)_Javascript_Axios_Fetch - Fatal编程技术网

Javascript 获取API返回415(不支持的媒体类型)

Javascript 获取API返回415(不支持的媒体类型),javascript,axios,fetch,Javascript,Axios,Fetch,我想用fetchapi(或unfetch)替换axios包,但由于某些原因,fetch对我不起作用。Axios工作正常,因此我将其替换为fetch,但返回的是415(不支持的媒体类型)。我使用它来上传一个文件到服务器,并发出POST请求。也许我在提取设置中缺少了什么 FormData const upload = file.length > 0 && file[0] formData.append('file', upload, upload.name) Axios(工作

我想用fetchapi(或unfetch)替换axios包,但由于某些原因,fetch对我不起作用。Axios工作正常,因此我将其替换为fetch,但返回的是
415(不支持的媒体类型)
。我使用它来上传一个文件到服务器,并发出POST请求。也许我在提取设置中缺少了什么

FormData

const upload = file.length > 0 && file[0]
formData.append('file', upload, upload.name)
Axios(工作)

提取(不工作)


我认为您缺少一个
Accept
标题:

const { data: { small, large } } = await fetch(`${API_URL}/upload/avatar`, {
  method: 'POST',
  body: formData,
  headers: {
    'Content-Type': 'multipart/form-data',
    'Authorization': `Bearer ${getTokenFromCookie()}`,
    'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
  },
})

只需从获取标题中删除
内容类型

const { data: { small, large } } = await fetch(`${API_URL}/upload/avatar`, {
  method: 'POST',
  body: formData,
  headers: {
    'Content-Type': 'multipart/form-data',
    Authorization: `Bearer ${getTokenFromCookie()}`,
  },
})

const { data: { small, large } } = await fetch(`${API_URL}/upload/avatar`, {
  method: 'POST',
  body: formData,
  headers: {
    'Content-Type': 'multipart/form-data',
    'Authorization': `Bearer ${getTokenFromCookie()}`,
    'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
  },
})