Vue怎么为GET或POST请求设置请求头
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,这篇文章主要介绍了Vue怎么为GET或POST请求设置请求头的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Vue怎么为GET或POST请求设置请求头文章都会有所收获,下面
千家信息网最后更新 2025年02月02日Vue怎么为GET或POST请求设置请求头
这篇文章主要介绍了Vue怎么为GET或POST请求设置请求头的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Vue怎么为GET或POST请求设置请求头文章都会有所收获,下面我们一起来看看吧。
为GET或POST请求设置请求头
安装vue-cookies
就通过我写的一个小项目的登录来讲vue-cookies,登陆成功后拿到后台返回的token值,把它保存到vue-cookies中
首先需要安装vue-cookies
npm install vue-cookies --save
使用
import Vue from 'vue'import VueCookies from 'vue-cookies'Vue.use(VueCookies);
设置cookies
this.$cookies.set("自定义名字",后台获取的token)
示例
//Login.vue Login(){ //登录 this.$http.post('home/login', {"uPwd":this.password,"uPhone":this.account}).then(result=>{ if(result.body.status===200){ this.$cookies.set("auth",result.body.data); let redirect=decodeURIComponent(this.$route.query.redirect||"/home"); this.$router.push({path:redirect}); }else{ Toast('输入密码或账号错误,请重新输入'); } }).catch(function(){ console.log("服务器异常"); }) } }
全局设置请求头
//index.jsVue.http.interceptors.push((request, next) => { //请求发送前的处理逻辑 request.headers.set('auth', VueCookies.get("auth")) next((response) => { //请求发送后的处理逻辑 //根据请求的状态,response参数返回给successCallback或errorCallback return response })})
vue项目设置请求头权限问题
新建一个公共js文件。如: lp.js.
//设置请求消息头export function headers(contentType,token){let headers={};headers['Content-Type'] = contentType ? contentType : 'application/json;charset=utf-8'; let a = _window.location.href; let b = a.indexOf("#"); let loginUrl = a.substring(b+2); if(loginUrl){ localStorage.loginUrl = loginUrl; } let url = a.substring(0,b+2); if(loginUrl==='login' || loginUrl==='reg' || loginUrl==='phoneAuth' || loginUrl=='emailInfo'){ token=''; }else{ token=window.localStorage.getItem('jingjing_login_token'); } token = token ? token : '';let key = 'jmjbGEWO4EyItpA4';let current_timestamp = new Date().getTime() + '';let nonce_str = getNonceStr(32);let lp_sign = sign(token, current_timestamp, nonce_str, key);headers['token'] = token;headers['current-timestamp'] = current_timestamp;headers['nonce-str'] = nonce_str;headers['lp-sign'] = lp_sign;return headers;
}//生成随机字符串export function getNonceStr(len) { len = len || 32; let chars = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; let maxPos = chars.length; let s = ''; for (let i = 0; i < len; i++) { s += chars.charAt(Math.floor(Math.random() * maxPos)); } return s;}// 在组件中使用直接引用,import {headers} from '@/assets/js/common/lp.js' methods: { // 获取员工架构 getTree() { // that.$emit('updataTree') let that = this this.$http({ method:"get", url:api.treeList, **headers:headers('application/json;charset=utf-8'),** cache:false }).then(function(res){ if(res.data.code==10000 || res.data.data==null){ that.treeDatas = res.data.data console.log(that.treeDatas,'/====treeDatas') }else{ that.$message.error(res.data.msg); } }); }, }
其中api.js中这么定义
let Butterfly="http://192.168.6.207:8891/jersey/";//xxipconst api = { baseUrl:Butterfly, treeList:Butterfly+'enterpriseInfo/current/user', };export default api;
当然在路由请求时需要设置,在router.js文件中
router.beforeEach((to, from, next) => { if(!localStorage.jingjing_login_token && to.path!=='/' && to.name !== 'phoneAuth' && to.name !== 'reg' && to.name !== 'agree' && to.name !== 'restPsd' && to.name !== 'ablityAssessment' && to.name !== 'leaveAssessment' && to.name !== 'contact' && to.name !== 'wasAsked' && to.name !== 'newWitesite_index' && to.name !== 'bySinging' && to.name !== 'brokenQuery' && to.name !== 'login'){ router.push({path:'/login'}); }else{ let a = _window.location.href; let b = a.indexOf("#"); let loginUrl = a.substring(b+2); if(loginUrl){ localStorage.loginUrl = loginUrl; } next(); }});export default router;
关于"Vue怎么为GET或POST请求设置请求头"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"Vue怎么为GET或POST请求设置请求头"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
知识
内容
后台
文件
篇文章
逻辑
处理
登录
输入
成功
价值
全局
参数
名字
员工
字符
字符串
密码
小项
操作简单
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
无数据库搜索技术
完美世界玩什么服务器人多
日活跃ip5万需要多大服务器
关于网络安全的动画图片
人事数据库中的字段
广联达数据库编码是多少
搭建云免流服务器视频
系部网络安全自查报告
梦飞云服务器安全性
知名的重庆电信服务器托管服务器
企业网络安全防护图
快速进入sae数据库
数据库管理系统的应用实例
支付宝网络安全观后感
河南省食蚁兽网络技术
利用视频数据库讲座心得体会
简单网络安全手抄报字少
文件连接数据库
惠州市网络技术 学校
手游红警服务器注册满了怎么办
江西高校党建软件开发公司
数据库系统哪个最重要
东北林业大学外文数据库
服务器优惠
软件开发主流编程语言
如何设计数据库的软件
安徽2u工控服务器机箱品牌
承接软件开发平台
肇庆通信软件开发
软件开发学校在哪里