Yifei Kong

Jun 14, 2017

axios and fetch

cookies

浏览器中的 JavaScript 运行在一个沙箱中。

fetch 默认不带 cookie, see https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials

axios 自动附加请求域名对应的 cookie

可以使用 JavaScript 设置对应所在域名的 cookie,而 chrome extension 可以设置所有域名的 cookie

basic usage

try {
    let response = axios.get(url)
} catch (e) {
    console.log(e)
}

// 可以使用的几种语法
axios.get(url)
axios.post(url, data)
axios(config)
axios(url, config)
axios.request(config)

config fields: params, data, headers, timeout, auth, response

或者创建一个实例

var instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  timeout: 1000,
  headers: {'X-Custom-Header': 'foobar'}
});

instance.get('/')

see

  • https://stackoverflow.com/questions/34558264/fetch-api-with-cookie