当前位置:网站首页>el-upload添加请求头
el-upload添加请求头
2022-07-30 22:51:00 【辰九九】
solution
Vue
项目,需要上传文件
task
使用elementUI
的el-upload
组件实现文件的上传
action
代码如下
<el-upload
class="upload-demo"
drag
action="http://47.92.112.6:8000/api/admin/calc/file"
:multiple="false"
:show-file-list="true"
accept=".rar,.zip"
:limit=1
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">只能上传rar/zip文件</div>
</el-upload>
上传文件报错 出现跨域问题,查看请求头因为没有携带token
,(项目中使用jwt
作为用户身份凭证)
于是增加请求头,查看官网API,需要增加 headers
请求头
修改代码如下,增加 headers
<el-upload
class="upload-demo"
drag
action="http://47.92.112.6:8000/api/admin/calc/file"
:multiple="false"
:show-file-list="true"
accept=".rar,.zip"
:limit=1
:headers="headers"
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">只能上传rar/zip文件</div>
</el-upload>
computed
内增加如下内容
computed: {
headers(){
const jwt=localStorage.getItem('jwt')
return{
jwt:jwt
};
},
获取存储在localStorage
中的jwt
,加载请求头上
调试,文件上传成功,请求头如下
result
在vue
中,computed
表示“计算属性”,是根据依赖关系进行缓存的计算,只有在它的相关依赖发生改变时才会进行更新,所以在computed
中获取jwt
,便于获得最新的数据
以前没有学过Vue
,直接上手写项目,多有不熟练之处,敬请指正
边栏推荐
猜你喜欢
Py之pdpbox:pdpbox的简介、安装、案例应用之详细攻略
MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat
Go1.18升级功能 - 泛型 从零开始Go语言
WSL安装图形界面并通过xrdp/X-Launch访问
打动中产精英群体,全新红旗H5用产品力跑赢需求
Reverse linked list - head insertion inversion method
鳄梨价格数据集(Avocado Prices)
reindex win10
MySQL索引常见面试题(2022版)
2022中国物流产业大会暨企业家高峰论坛在杭州举办!
随机推荐
只会纯硬件,让我有点慌
成功解决ModuleNotFoundError: No module named ‘Image‘
2022.7.27
Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)
MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat
电脑快捷方式图标变白解决方案
reindex win10
Abstract classes and interfaces (study notes)
[MySQL] Related operations on databases and tables in MySQL
MySQL索引常见面试题(2022版)
2021GDCPC Guangdong University Student Programming Competition B.Byfibonacci
#Dasctf July Enabler WP
Chapter 8 Intermediate Shell Tools II
IJCAI2022教程 | 口语语言理解:最新进展和新领域
Debezium报错系列之二十:task failed to create new topic.Ensure that the task is authorized to create topics
2021GDCPC广东省大学生程序设计竞赛 B.Byfibonacci
1064 Complete Binary Search Tree
OpenCV笔记(二十):滤波函数——filter2D
【2022-05-31】JS逆向之易企秀
Computer shortcut icon whitening solution