当我尝试在分页后通过Ajax发送数据时,400 Bad Request调用

时间:2016-12-07 04:19:40

标签: jquery html ajax django

我对编码很新,所以如果不清楚我会道歉。我已经用Django创建了一个测验应用程序,它可以为任何问题提供“是”或“否”的答案,如果没有给出答案,则会弹出一个模态,要求用户提供更多详细信息。当用户点击提交时,此模式通过ajax调用将信息传递回数据库。在第一个问题上一切正常,但在第二个问题加载后,我得到一个400(错误请求)错误,控制台显示我的所有隐藏字段的初始条件都是空白的。我以前通过在每次提交后刷新页面来解决这个问题,但这并没有保存我对数据库的回答,只允许我传递模态信息。这是我的ajax的代码

        $(document).ready(function(){           
    $("#submit-btn").click(function(event){
        event.preventDefault();
        var formData = $("#question-form").serializeArray();
        var indexed_array = {};

        $.map(formData, function(n, i){
            indexed_array[n['name']] = n['value'];
        });
        var answer = indexed_array.answer_id;
        console.log(answer);
        if (answer != 2) {
            $("#question-form").submit()
        }           
        else {
            $("#questionModal").modal("show");    

           }
        $("#sendDiscrepancyForm").click(function(e){
            e.preventDefault()    
            var mForm = $("#sendForm").serialize()
            $.ajax({
                type: "POST",
                url: "{% url 'ajax_hit_list'  %}",
                data: mForm,              
                success: function(data){
                    console.log(data)
                    $("#modalMessage").html("<p>This was Successful</p>")

                       $("#questionModal").modal("hide")
                        $("#question-form").submit()
                            location.reload()
                },
                error: function(data){ 
                    var obj = data.responseJSON
                    console.log(obj)
                    $("#modalMessage").html("<p>Please Fill out All Fields!</p>");
                }
            });

        });
    });        
});

这是我的模态

的代码
    <div class="modal fade" id="questionModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">

        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Discrepancy</h4>
      </div>
      <div class="modal-body">
        {% if queryset.has_next %}
        <form id ='sendForm' method='POST' action='{% url "ajax_hit_list" %}'>{% csrf_token %}
        {% else %}
        <form id ='sendForm' method='POST' action=''>{% csrf_token %}
        {% endif %}
        <span>

           {{ hitform.user.as_hidden }}
            {{ hitform.ships.as_hidden }}
            {{ hitform.ships.as_hidden }}
            {{ hitform.system.as_hidden }}
            {{ hitform.discover_date.as_hidden }}
            <h4>Department: {{ hitform.department }}</h4>
            <p id='modalMessage'></p>
            <h4>Description: {{ hitform.description }}</h4>
            <h4>Repair Schedule: {{ hitform.repair_schedule }}</h4>
            <h4>SAT: {{ hitform.sat }}</h4>
            <h4>STO: {{ hitform.sto }}</h4>
            <h4>JSN: {{ hitform.jsn }}</h4>         
        </span>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <input type="submit" class="btn btn-primary" id="sendDiscrepancyForm">
      </form>  
      </div>
    </div>
  </div>
</div>

以下是我尝试将数据从模态传递到

的视图
def discrepancy_modal(request, **kwargs):
hitform = DiscrepancyForm(request.POST or None)
today = date.today()
if request.method == "POST":
    if hitform.is_valid():
        ships = hitform.cleaned_data['ships']
        system = hitform.cleaned_data['system']
        description = hitform.cleaned_data['description']           
        department = hitform.cleaned_data['department']
        repair_schedule = hitform.cleaned_data['repair_schedule']
        sat = hitform.cleaned_data['sat']
        sto = hitform.cleaned_data['sto']
        jsn = hitform.cleaned_data['jsn']

        new_discrepancy = Discrepancy()
        new_discrepancy.user = request.user
        new_discrepancy.ships = ships
        new_discrepancy.system = system
        new_discrepancy.department = department

        new_discrepancy.description = description
        new_discrepancy.discover_date = today
        new_discrepancy.repair_schedule = repair_schedule
        new_discrepancy.sat = sat
        new_discrepancy.sto = sto
        new_discrepancy.jsn = jsn
        new_discrepancy.save()
        return HttpResponse('Success')
    if hitform.errors:
        print hitform.errors
        json_data = json.dumps(hitform.errors)
        return HttpResponseBadRequest(json_data, content_type='application/json')
    else:
        raise Http404
context = {
    "hitform": hitform,
}
return render(request, "form.html", context)

0 个答案:

没有答案