简体   繁体   English

Flask-使用Javascript / JQuery / Ajax提交表单(文本区域,文件等)

[英]Flask - Submit a form (text area, files, …) with Javascript/JQuery/Ajax

I would like to use JQuery/Javascript/Ajax to send what's in the form (text area, files, ...) to one function of my Flask app. 我想使用JQuery / Javascript / Ajax将表单(文本区域,文件等)中的内容发送到Flask应用程序的一个功能。

<form action='' method="post">
       <label for="label">Example</label><br>
       <textarea name="text_area" id="text_area" rows="10" cols="100">
       </textarea><br>
       <i>Upload a file : <input type='file' id="file_query" name='file_query'></i>
       <br><br>
       <a id="submit_all">Submit</a> 
</form>

I would like to be able to access what have been sent in request.form and request.files 我希望能够访问在request.form和request.files中发送的内容

How to do it and keep it simple ? 如何做到并保持简单?

I did follow what @Jai said in his comment : "use FormData with ajax" 我确实遵循@Jai在他的评论中所说的: "use FormData with ajax"

The following worked and I was able to get back the data inside my function. 以下工作正常,我能够取回函数中的数据。

$(function() {
    $('#submit_btn').on('click', function(){
       var fd = new FormData(document.querySelector("form"));
       $.ajax({
          url: "/_apply_function",
          type: "POST",
          data: fd,
          processData: false,  // tell jQuery not to process the data
          contentType: false   // tell jQuery not to set contentType
        });
      });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM