Ajax发布表单数据

时间:2013-09-13 12:00:44

标签: ajax forms jquery post form-data

我有以下表格

<form id="reportcriteriaform">
   <div id="reportdialog_dialog" title="New Report" link="/newreport" class="modal show fade in" style="overflow:hidden;text-align:center;" aria-hidden="false">
      <div class="modal-header">
         WI's Resolved Report
      </div>
      <div class="modal-body">
         <div class="row-fluid">
            <div class="span6">
               <!-- Main report criteria fields go here -->            
               <div id="reportStartDate_row" class="control-group text-left">
                  <label class="control-label bold" for="reportStartDate"> <label for="reportStartDate">Start Date</label> </label> 
                  <div class="controls">
                     <div class="input-append date datepicker control-group" data-date-format="dd-M-yyyy">     
                        <input type="text" id="reportStartDate" name="reportStartDate" data-date-format="dd-M-yyyy" bs-datepicker="" class="input-large" value="">
                        <span class="add-on"><i class="icon-th" id="fromBtn"></i></span>
                     </div>
                  </div>
               </div>
               <div id="reportEndDate_row" class="control-group text-left">
                  <label class="control-label bold" for="reportEndDate"> <label for="reportEndDate">End Date</label> </label> 
                  <div class="controls">
                     <div class="input-append date datepicker control-group" data-date-format="dd-M-yyyy">     
                        <input type="text" id="reportEndDate" name="reportEndDate" data-date-format="dd-M-yyyy" bs-datepicker="" class="input-large" value="">
                        <span class="add-on"><i class="icon-th" id="fromBtn"></i></span>
                     </div>
                  </div>
               </div>
               <!-- Maultiselect box goes here -->                     
            </div>
            <div class="span6">
               <p>
                  <button type="button" name="runQuery" id="runQuery" class="btn btn-mini btn-primary " value="">Run Query</button>
               </p>
               <p>
                  <button type="button" name="close" id="close" class="btn btn-mini btn-primary " value="">Close</button>
               </p>
            </div>
         </div>
      </div>
   </div>
</form>

当我使用以下ajax调用提交它时,我在request.data或request.form中看不到任何数据

$.ajax({
    url: '/newreport',
            type:'POST',
    data:$('#reportcriteriaform').serialize(),  
    dataType: 'text/html',             
    success: function(data) {
                       //   Success stuff           
        }
});

newreport.py

from flask import render_template, request, session
    def post(self):
            request.data //This is empty string
            request.form //This is empty dictionary

            #Other stuff

1 个答案:

答案 0 :(得分:0)

在ajax电话中 它应该

type:'POST'

你错过了引号。

并且根据标准数据类型也提到数据类型为"text/html"。(虽然没有引起问题,但是按照标准规范保持代码的良好做法)