最近的项目中遇到一个问题,请求只允许GET和POST,但是使用Element-UI的上传时会默认触发options预请求,导致请求失败,查阅了一些资料.终于解决了,先记录下来.
html部分:action一定设置#,其他的按照正常用法写, 点击上传的事件写在before-upload事件中即可
<el-upload
class="upload-demo"
action="#"
:file-list="fileList"
:on-preview="handlePreview"
:on-remove="handleRemove"
:before-upload="beforeUpload"
:on-success='onSuccess'
:on-error='onError'
multiple="true"
:data="formData"
>
<el-button size="mini" type="primary">上传</el-button>
</el-upload>
js部分:请求用的jquery的方法,别的也类似,获取到文件,文件append到FormData中,别的参数也一样,
请求中必须要写的是**processData:false**,**contentType:false**.
beforeUpload(file){
let fd = new FormData()
fd.append('file',file)
$.ajax({
processData:false,
contentType:false,
url: url,
type: "POST",
async: false,
dataType:"JSON",
data: fd,
success:(res)=>{
},
error:(err)=>{
}
})
},
这样就可以开心快乐的传送文件了