https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
Response.ok
请注意,fetch 规范与 jQuery.ajax() 主要有以下的不同:
- 当接收到一个代表错误的 HTTP 状态码时,从 fetch() 返回的 Promise 不会被标记为 reject,即使响应的 HTTP 状态码是 404 或 500。相反,它会将 Promise 状态标记为 resolve(如果响应的 HTTP 状态码不在 200 - 299 的范围内,则设置 resolve 返回值的 ok 属性为 false),仅当网络故障时或请求被阻止时,才会标记为 reject。
- fetch 不会发送跨域 cookie,除非你使用了 credentials 的初始化选项。(自 2018 年 8 月以后,默认的 credentials 政策变更为 same-origin。Firefox 也在 61.0b13 版本中进行了修改)
项目案列
koa项目
const fetch = require('isomorphic-unfetch');
https://stackoverflow.com/questions/61483803/what-is-the-difference-between-isomorphic-fetch-and-isomorphic-unfetch-npm-p
The isomorphic-fetch is built on top of whatwg-fetch and isomorphic-unfetch is built on top of unfetch. What both of them are doing is switching between node-fetch and the other package for client and server. So their difference is only in the client and the question reduces to: What’s the difference between whatwg-fetch and unfetch? The whatwg-fetch package is an almost complete polyfill for fetch API, but the unfetch is built with “bundle size” as the first priority in mind (like the other packages written by Jason Miller such as preact). The unfetch only supports a subset of the full fetch API. However, it’s great if you just need simple requests in your application. A good strategy for new applications is to use unfetch at first, and replace it with whatwg-fetch if you need any feature (such as streaming) which is not supported in unfetch.
配置本地代理:
https://github.com/matthew-andrews/isomorphic-fetch/issues/171
参考nextjs项目中使用http-proxy-agent