fetch返回值 then后提示错误TypeError: Cannot read properties of undefined (reading 'json)

27 阅读1分钟

代码:


fetch(url,{
      method:'post',
      body:JSON.stringify(Data),
      headers: {
        'Content-Type': 'application/json',
      }
    },
).then(res=>{
  return res.json()  //错误代码
}).then(json=>{
  console.log(json)
}).catch(err=>{
  console.log(err)
})

当后端返回的数据是String格式的时候就不能使用res.json(),否则报错

服务器返回值:

res.send('message')

浏览器错误信息: TypeError: Cannot read properties of undefined (reading 'json)

将错误代码改为

return res.text()