Problems with el-upload uploading files using axios

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:
clipboard.png
getpost:

clipboard.png

ask why?

Note: if there is no login verification before calling the interface of that layer, there is no problem with file upload

Apr.05,2021

this is the logic that browsers do when dealing with cross-domain processing. The CORS cross-domain request will send the option request first. If the access-control-allow-origin header returned by server is * or is consistent with the current domain name, the real request in the second paragraph will be entered. Otherwise, an cross origin request is forbidden error will be reported.

author: slashhuang
Link: https://www.zhihu.com/questio.
Source: Zhihu
copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.


Oh, you must be stuck by authorization. If you want to go to passport, you should carry token when uploading (specifically see verification logic and verification logic). It is common that you do not upload additional users token


to the upload component.
I delete the with-credentials= "true" of the el-upload component itself, and I can post normally. Although I don't know why.


Why am I wrong when I refer to you? content can't find
TypeError: Cannot read property 'file' of undefined

.
Menu