Fetch API和Axios是否将304响应视为200? [英] Do the Fetch API and Axios see 304 responses as 200?

查看:119
本文介绍了Fetch API和Axios是否将304响应视为200?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否可以,但是如果我的服务器以HTTP状态代码304响应,则Fetch API(特别是 response.ok )和 axios.get()看到响应为200?

I don't know if it ever would, but if my server responded with HTTP status code 304, would the Fetch API (specifically response.ok) and axios.get() see the response as 200?

如果响应代码在200-299范围内,但显然304不在此范围内,则这两个文档都认为请求被视为成功.

The documentation for both talk about the request being viewed as successful if the response code is in the range 200-299, but clearly 304 is outside this.

推荐答案

当浏览器使用 If-Match If-Modified-Since 标头进行GET请求时,并且服务器以 304 Not Modified 进行响应,客户端将其视为 200 OK .

When a browser does a GET request with a If-Match or If-Modified-Since header, and the server responds with 304 Not Modified, the client will just see this as 200 OK.

响应是从缓存而不是服务器提供的,它使客户端(在您的情况下为axios)不必了解HTTP缓存,但仍然可以利用它.

The response is served from cache, instead of the server, and it allows the client (axios in your case) to not have to understand HTTP caching, but it can still take advantage of it.

我不知道客户端在没有先决条件的情况下发送请求并仍然得到 304 响应时会做什么.客户端将没有较早的缓存响应,因此这肯定是中断"的.我以为您会出错,但我很好奇看到了什么.

I don't know what a client will do when they send a request without preconditions and still get a 304 response. A client wouldn't have an earlier cached response so this would definitely be 'broken'. I'd imagine you'd get an error but I'd be curious to see what.

这篇关于Fetch API和Axios是否将304响应视为200?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆