I use axios instead of the default file upload of the el-upload component. When calling the upload API, the API call will be first blocked by login verification to check whether it is logged in. When I clicked upload, I found that the upload interface was called twice, once for option, and once for get (not post, is very strange), and then prompted for cross-domain.
other APIs related to get requests can be solved by bringing cookies with axios requests. There is no cross-domain problem, but there is a problem with file upload here. When I deploy the interface to the test environment (no login verification), it works fine.
my code is as follows:
<template>
<el-upload
class="upload-demo"
ref="upload"
:action="uploadAction"
:http-request="myUpload"
:on-success="handleSuccess"
:on-error="handleError"
:auto-upload="false"
:with-credentials="true">
<el-button slot="trigger" size="small" type="primary" icon="el-icon-document"></el-button>
<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload" icon="el-icon-upload"></el-button>
<div slot="tip" class="el-upload__tip"><b>apk</b>"<b>_76</b>" <br/></div>
</el-upload>
</template>
next is the definition of myUpload:
myUpload(content) {
var form = new FormData();
form.append("file", content.file);
this.$axios.post(content.action, form).then(res=>{
if(res.data.code != 0) {
content.onError(", code:" + res.data.code)
} else {
content.onSuccess("")
}
}).catch(error=>{
if (error.response) {
content.onError("," + error.response.data);
} else if(error.request) {
content.onError("")
} else {
content.onError("")
}
});
}
will receive two requests:
once option:
getpost:
ask why?
Note: if there is no login verification before calling the interface of that layer, there is no problem with file upload