Javascript 如何使用fetch处理response.json和.text?

Javascript 如何使用fetch处理response.json和.text?,javascript,fetch,es6-promise,Javascript,Fetch,Es6 Promise,我正在获取一个返回json的API,但是当它有错误时,它只返回一个文本(在使用express的节点中,结果使用.json({})返回,错误使用.send('string')),但是我无法修改API 所以我试图制作一些读取json的东西,但是如果它是文本,它将进入.catch,其中错误是文本 这是我尝试过的,但没有成功 fetch(apiUrl) .then(res => { try { let json = res.json()

我正在获取一个返回json的API,但是当它有错误时,它只返回一个文本(在使用express的节点中,结果使用
.json({})
返回,错误使用
.send('string')
),但是我无法修改API

所以我试图制作一些读取json的东西,但是如果它是文本,它将进入
.catch
,其中错误是文本

这是我尝试过的,但没有成功

fetch(apiUrl)
    .then(res => {
        try {
            let json = res.json()
            return json
        } catch (error) {
            return new Promise((resolve, reject) => reject(res.text()))
        }
    })
    .then(res => {
        // get result from res.json() **res == res.json**
    })
    .catch(error => {
        // get result from res.text() **res == res.text**
    })
我怎样才能做到这一点?如何在下一个
中获取
res.json()
。然后()
但如果失败,则在
中获取
res.text()

编辑:
我想在
.catch
中获取
.text
。我不知道为什么,但是抛出
res.text()
不起作用

理想情况下,您的客户端应用程序应该知道所期望的响应类型,并具有调用适当方法的静态代码


处理这种情况的另一种方法是检查响应
contentType
,并根据特定的响应头值调用
.json()
.text()

handleResponseStatusAndContentType(response) {
  const contentType = response.headers.get('content-type')!;

  if (response.status === 401) throw new Error('Request was not authorized.');

  if (contentType === null) return Promise.resolve(null);
  else if (contentType.startsWith('application/json;')) return response.json();
  else if (contentType.startsWith('text/plain;')) return response.text();
  else throw new Error(`Unsupported response content-type: ${contentType}`);
}
用法:

return fetch(
  url,
  requestInit,
)
.then(response => handleResponseStatusAndContentType(response))
.catch(error => {
  console.error(error);
  return error;
});

另一种方法是最初将eveyrthing格式化为文本,然后再尝试解析它,同时在解析问题时抛出错误

fetch(“http://maps.googleapis.com/maps/api/geocode/json?address=google")
。然后(res=>res.text())
。然后(body=>{
试一试{
返回JSON.parse(body);
}抓住{
投掷误差(体);
}
})
.then(console.log)
.catch(控制台错误);
取回(“http://maps.googleapis.com/maps/api/geocode/xml?address=google")
。然后(res=>res.text())
。然后(body=>{
试一试{
返回JSON.parse(body);
}抓住{
投掷误差(体);
}
})
.then(console.log)

.catch(控制台错误)新承诺的语法是什么?它看起来像模板/泛型,但JS没有那个功能。@IceMetalPunk道歉。当我把我的TypeScript代码翻译成JavaScript时,我错过了这一部分……我刚刚意识到这是不正确的。如果它只是一个文本,它将进入下一个
。然后
但不在
中。catch
@Vencovsky不确定你的意思。在你的代码中返回一个解析为null的承诺:
新承诺(()=>null)
是不正确的,因为它只会返回一个永远无法解析的承诺,而尝试
promise.resolve(null)
我喜欢这个解决方案。