axios如何配置携带cookie进行koa请求?
- 内容介绍
- 文章标签
- 相关推荐
本文共计830个文字,预计阅读时间需要4分钟。
言简意赅,一字,干!+ 前端配置如下:+ axios.defaults.withCredentials=true;+ // 设置为true+ axios.post('http://localhost:3000/tpzdz/vote/all', {+ openid: 'oJ0mVw4QrfS603gFa_uAFDADH2Uc',+ date: '2018-11-21'+ });
话不多说,一个字,干!
前端配置如下:
axios.defaults.withCredentials = true; //配置为true axios.post('localhost:3000/tpzdz/vote/all', { openid: 'oJ0mVw4QrfS603gFa_uAFDADH2Uc', date: '2018-11-21' }).then(function (response) { console.log(response) })
前端配置withCredentials = true 后端的跨域也需要配置
app.use(async (ctx, next) => { ctx.set('Access-Control-Allow-Origin', ctx.request.header.origin); ctx.set('Access-Control-Allow-Credentials', true); await next(); }); //防止每次请求都返回Access-Control-Allow-Methods以及Access-Control-Max-Age, //这两个响应头其实是没有必要每次都返回的,只是第一次有预检的时候返回就可以了。
本文共计830个文字,预计阅读时间需要4分钟。
言简意赅,一字,干!+ 前端配置如下:+ axios.defaults.withCredentials=true;+ // 设置为true+ axios.post('http://localhost:3000/tpzdz/vote/all', {+ openid: 'oJ0mVw4QrfS603gFa_uAFDADH2Uc',+ date: '2018-11-21'+ });
话不多说,一个字,干!
前端配置如下:
axios.defaults.withCredentials = true; //配置为true axios.post('localhost:3000/tpzdz/vote/all', { openid: 'oJ0mVw4QrfS603gFa_uAFDADH2Uc', date: '2018-11-21' }).then(function (response) { console.log(response) })
前端配置withCredentials = true 后端的跨域也需要配置
app.use(async (ctx, next) => { ctx.set('Access-Control-Allow-Origin', ctx.request.header.origin); ctx.set('Access-Control-Allow-Credentials', true); await next(); }); //防止每次请求都返回Access-Control-Allow-Methods以及Access-Control-Max-Age, //这两个响应头其实是没有必要每次都返回的,只是第一次有预检的时候返回就可以了。

