我有一個用 Python 編寫的服務器 Flask:# configurationDEBUG = TrueUPLOAD_FOLDER = './uploads/'# instantiate the appapp = Flask(__name__)app.config.from_object(__name__)app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER # enable CORSCORS(app, resources={r'/*': {'origins': '*'}})#Upload@app.route('/')@app.route('/api/uploadFile',methods=['POST'])def uploadFile(): response_object = {'status': 'success'} response_object['message'] = 'Caricamento effettuato con successo' file = request.files['file'] filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename)) return jsonify(response_object) # sanity check route@app.route('/ping', methods=['GET'])def ping_pong(): return jsonify('pong')if __name__ == '__main__': app.run()我有一個上傳文件的 Vuejs 組件:<template> <div> <form action = "http://localhost:5000/api/uploadFile" method = "post" enctype="multipart/form-data"> <input type="file" id="file_id" name="file" v-on:change="onFileChange" value="Choose file" /> <button class="btn btn-success" @click="upload">Upload</button> </form> </div></template><script>import axios from 'axios';export default { name: 'UploadFile', data() { return { file: null, }; }, methods: { created() { this.getBooks(); }, onFileChange(e) { const files = e.target.files || e.dataTransfer.files; if (!files.length) { return; } this.createFile(files[0]); }, createFile(file) { const reader = new FileReader(); const vm = this; reader.onload = (e) => { vm.file = e.target.result; }; reader.readAsDataURL(file); },一切正常(上傳工作正常),但 axios.post 調(diào)用的響應沒有出現(xiàn)在控制臺中!瀏覽器告訴我這個:對于顯示問題的圖像,請單擊此處1我該如何解決這種情況?非常感謝您的幫助
Vuejs 向燒瓶服務器發(fā)送請求,更改 url 并返回原始 json
桃花長相依
2022-10-27 15:14:14