在 axios 和 Vue 中使用 cookie
Using cookies with axios and Vue
我创建了一个 Node.js 快速服务器,它使用 'jsforce' 提供的 SOAP 接口连接到 Salesforce.com。它通过 'express-session' 包使用会话 cookie 进行授权。到目前为止,它有一个用于登录的 POST 方法和一个用于执行简单查询的 GET 方法。使用 Postman 进行测试证明此服务器按预期工作。
作为此服务器的浏览器界面,我编写了一个使用 axios 执行 GET 和 POST 的 Vue 应用程序。我需要保存登录期间创建的会话 cookie POST,然后将 cookie 附加到后续的 CRUD 操作。
我试过各种方法来处理cookies。我尝试过的一种方法是在 POST
上使用 axios 响应拦截器
axios.interceptors.response.use(response => {
update.update_from_cookies();
return response;
});
函数 'update_from_cookies' 试图获取名为 'js-force' 的 cookie,但它没有找到它,尽管我知道它正在发送
import Cookie from 'js-cookie';
import store from './store';
export function update_from_cookies() {
let logged_in = Cookie.get('js-force');
console.log('cookie ' + logged_in);
if (logged_in && JSON.parse(logged_in)) {
store.commit('logged_in', true);
} else {
store.commit('logged_in', false);
}
}
我也看到了各种向 axios 调用添加参数的建议,但这些也不起作用。
关于如何使用 axios 或与 Vue 一起使用的类似包处理 cookie 的一些建议,我将不胜感激
谢谢
问题已解决。我对 axios 调用使用了错误的语法
正确的语法将 {withCredentials: true} 作为最后一个参数
this.axios.post(uri, this.sfdata, {withCredentials: true})
.then( () => {
this.$router.push( {name : 'home' });
})
.catch( () => {
});
我创建了一个 Node.js 快速服务器,它使用 'jsforce' 提供的 SOAP 接口连接到 Salesforce.com。它通过 'express-session' 包使用会话 cookie 进行授权。到目前为止,它有一个用于登录的 POST 方法和一个用于执行简单查询的 GET 方法。使用 Postman 进行测试证明此服务器按预期工作。
作为此服务器的浏览器界面,我编写了一个使用 axios 执行 GET 和 POST 的 Vue 应用程序。我需要保存登录期间创建的会话 cookie POST,然后将 cookie 附加到后续的 CRUD 操作。
我试过各种方法来处理cookies。我尝试过的一种方法是在 POST
上使用 axios 响应拦截器axios.interceptors.response.use(response => {
update.update_from_cookies();
return response;
});
函数 'update_from_cookies' 试图获取名为 'js-force' 的 cookie,但它没有找到它,尽管我知道它正在发送
import Cookie from 'js-cookie';
import store from './store';
export function update_from_cookies() {
let logged_in = Cookie.get('js-force');
console.log('cookie ' + logged_in);
if (logged_in && JSON.parse(logged_in)) {
store.commit('logged_in', true);
} else {
store.commit('logged_in', false);
}
}
我也看到了各种向 axios 调用添加参数的建议,但这些也不起作用。
关于如何使用 axios 或与 Vue 一起使用的类似包处理 cookie 的一些建议,我将不胜感激
谢谢
问题已解决。我对 axios 调用使用了错误的语法
正确的语法将 {withCredentials: true} 作为最后一个参数
this.axios.post(uri, this.sfdata, {withCredentials: true})
.then( () => {
this.$router.push( {name : 'home' });
})
.catch( () => {
});