POST请求的行为不符合预期

时间:2017-12-12 16:26:43

标签: ajax django

我一直试图让我的按钮向提交的URL发送POST请求,该URL会写回数据库。应用程序看起来像POST请求被发送,但是在点击按钮后,我的URL永远不会改变,并且提交的URL处的打印看起来是空集。

这是我对按钮的jquery / ajax调用:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>


<button class="btn btn-primary my_select" type="submit">Request Access</button>
              <script>
              $(document).ready(function () {
                  $('form').on('submit', function (e) {
                      e.preventDefault();
                      var phi = $('#phi').val();
                      var accesslevelid = $('#accesslevelid').val();
                      $.ajax({
                          url: "{% url 'submitted' %}",
                          headers: { 'X-CSRFToken': '{{ csrf_token }}' },
                          data: {
                              phi: phi,
                              accesslevelid: accesslevelid,
                            },
                          type: 'POST',
                          success: function (result) {
                              // do something with result
                            },
                        });
                    });
                });
              </script>

我期待我的应用程序列表,PHI标志和访问级别的POST作为POST发送到我提交的URL。我提交的观点如下:

 def submitted(request):
    owner = User.objects.get (formattedusername=request.user.formattedusername)
    checkedlist = request.POST.getlist('report_id')

    coid = User.objects.filter(coid = request.user.coid).filter(formattedusername=request.user.formattedusername)
    facilitycfo =  QvDatareducecfo.objects.filter(dr_code__exact = coid, active = 1, cfo_type = 1).values_list('cfo_ntname', flat = True)
    divisioncfo =  QvDatareducecfo.objects.filter(dr_code__exact = coid, active = 1, cfo_type = 2).values_list('cfo_ntname', flat = True)

    print (f"checkedlist prior to post:{checkedlist}")

    selectedaccesslevel = request.POST.get('accesslevelid')


    selectedphi = request.POST.get('phi')



    if request.method == 'POST':
        for i in checkedlist:
            requestsave = QVFormAccessRequest(ntname = owner.formattedusername, first_name = owner.first_name, last_name = owner.last_name, coid = owner.coid, facility = owner.facility, title = owner.title
                                      ,report_id = i, accesslevel_id = selectedaccesslevel, phi = selectedphi , access_beg_date = '2017-01-01 00:00:00', access_end_date = '2017-01-31 00:00:00')
            requestsave.save()
            print (f"postlist:{checkedlist}")
            print (f"accesslevel:{selectedaccesslevel}")
            print (f"phi:{selectedphi}")


    return JsonResponse({'is_success':True})

当我按下按钮时,我的帖子看起来就像是这样:

[] [12 / Dec / 2017 08:54:45]“POST / account / submitted / HTTP / 1.1”200 1149

但是,URL不会切换为已提交。我的清单列表似乎是一个空集。当访问提交并且我的打印语句发生时,我得到的结果就像从未发生过POST一样。

我的表单操作如下:

<form action = "{% url 'submitted' %}" form method = "POST">
  {% csrf_token %}
    {{ form.as_p}}

1 个答案:

答案 0 :(得分:2)

构建ajax来执行后台客户端服务器操作或加载页面动态部分以获取大量请求

实施例

大多数社交媒体Feed都使用ajax。当您滚动视图时,会向服务器发送ajax请求以检索下一个Feed。

在您的情况下,数据已成功发布到服务器。但此时服务器无法更改URL,但您可以通过技巧实现此目的... 在您的a文件中

view.py

from django.http import JsonResponse if request.method == 'POST': for i in checkedlist: requestsave = QVFormAccessRequest(ntname = owner.formattedusername, first_name = owner.first_name, last_name = owner.last_name, coid = owner.coid, facility = owner.facility, title = owner.title ,report_id = i, accesslevel_id = selectedaccesslevel, phi = selectedphi , access_beg_date = '2017-01-01 00:00:00', access_end_date = '2017-01-31 00:00:00') requestsave.save() print (checkedlist) print(selectedaccesslevel) print(selectedphi) return JsonResponse({'is_sucess':True}) 对象将发回数据ajax。

JsonResponse

这个工作可以使用直接发布到网址然后重定向到其他网址来完成。现在就离开这个