获取但仅获取状态?

Fetch but only get status?

在附加新域时,我做了一个逻辑,通过fetch检查域是否正常工作。

fetch("https://" + domain).then((result)=>{
const isOk = result.status === 200
...
 })

但是,我认为通过 fetch 获取整个 HTML 文档只是为了检查它是否有效,效率很低。 我怎样才能只获得状态值?

您只能请求 headers,而不是整个页面,使用 HEAD method. The server should return the same headers as it would if the GET method was used, but no body. You can request this behaviour using the method option:

fetch(url, { method: 'HEAD' })