Cookie 携带问题
-
fetch在默认情况下, 不管是同域还是跨域ajax请求都不会带上cookie, 只有当设置了 credentials 时才会带上该cookie, 服务端需要设置响应头 Access-Control-Allow-Credentials: true, 否则浏览器会因为安全限制而报错, 拿不到响应
-
fetch 请求携带cookie
fetch(url, {
credentials: "include", // include, same-origin, omit
})
// include: 跨域ajax带上cookie
// same-origin: 仅同域ajax带上cookie
// omit: 任何情况都不带cookie
- axios和jQuery在同域ajax请求时会带上cookie, 跨域请求不会, 跨域请求需要设置 withCredentials 和服务端响应头
- axios 跨域请求 携带cookie
axios.get('http://server.com', {withCredentials: true})
- jquery 跨域请求 携带cookie
$.ajax({
method: 'get',
url: 'http://server.com',
xhrFields: {
withCredentials: true
}
})