【问题标题】:Redirect after POST request with jquery使用 jquery 在 POST 请求后重定向
【发布时间】:2011-10-01 06:11:46
【问题描述】:

我正在使用 Django。我有一个 HTML 页面,我在其中做一些 Javascript 的东西,然后我用这种方式做一个 jQuery 帖子:

$.ajax({ 
  url: '/xenopatients/measurement/qual', 
  type: 'POST', 
  data: {'obj':data}, 
  dataType: 'json', 
  contentType: "application/json; charset=utf-8", //questo ok 
}); 

在这个 post 请求之后,我的 Django 视图正确地处理了对该 URL 的调用。 我想要它做的是处理数据,将用户发送到另一个页面,然后将此数据发送到新页面。 问题是我不能像往常一样在 Python 中执行重定向,就像代码忽略了重定向一样。

我的 Python 代码是:

@csrf_protect 
@login_required#(login_url='/xenopatients/login/') 
def qualMeasure(request): 
    name = request.user.username 
    print "enter" 
    if request.method == 'POST': 
        print request.POST 

        if "obj" in request.POST: 
            print 'obj received' 
            return render_to_response('mice/mice_status.html', RequestContext(request)) 

    return render_to_response('measure/qual.html', {'name': name, 'form': QualMeasureForm()}, RequestContext(request)) 

我发现更改页面的唯一方法是在上面的代码之后通过 Javascript:

top.location.href = "/xenopatients/measurement";

但是我不知道如何在使用这个方法时传递我需要的数据。

HTML 代码:

<form action="" method=""> 
  <table id="dataTable" width="100%" border="1"></table><br> 
  <script language="javascript"> 
    document.measureForm.id_barcode.focus(); 
    document.measureForm.Add.disabled = false; 
    $('#dataTable').tablePagination({}); 
  </script> 
  <input type="button" name="save" value="Save Measure Serie" onclick="table2JSON('dataTable')"/> 
</form> 

附:我也试过$.post,但结果相同。

如何在 Django 中使用 jQuery 进行发布请求后进行重定向?

【问题讨论】:

  • 如果你想重定向,你为什么用 Ajax 做呢?为什么不以正常方式提交表单?使用 Ajax 的主要原因是避免重定向。
  • 我使用 ajax 来动态更改页面数据并将复杂数据发送到服务器......而且我不是独一无二的! ;)
  • python代码的可读性不是很好,因为标识有问题...
  • @acidjunk 谢谢,我从未见过那个错误。现在我已经修好了。 :)

标签: jquery ajax django post redirect


【解决方案1】:

好的,我找到了神奇的解决方案! :)

情况: jQUery.ajax() 调用了一个视图的方法

为了在 Ajax 之后重定向,我使用了 here 的提示(在 jQuery 选择器中使用文档而不是“正文”)

但是,我还有进一步的手术要做。

在被调用的视图中,调用另一个方法,像这样:

[...]
if request.method == 'POST':
    if "obj" in request.POST: #ricevuti dati da ajax
        request.session['qual'] = request.POST['obj']
        return HttpResponseRedirect(reverse("xenopatients.views.qualReport"))

我在会话中保存我需要的数据。我在被调用的视图中使用这些数据。

调用另一个方法是重定向浏览器页面的关键。 其实在被调用方法的最后,可以正常写&执行:

return render_to_response('measure/qualReport.html', {'name':name, 'list_report': report, 'message':'Measures correctly saved'}, RequestContext(request))

希望这对某人有用! ;)

【讨论】:

    【解决方案2】:

    @Daniel 是正确的,因为您希望避免使用 ajax 调用进行重定向,但您可以看看这个 Stack Overflow 问题,它解决了如何做到这一点:How to manage a redirect request after a jQuery Ajax call

    【讨论】:

    • 我认为这就像我的解决方案..在您的链接中我找不到对我有用的信息..没有关于通过 js 在帖子中发送数据或在帖子后通过 python 重定向的信息! :(
    【解决方案3】:

    点击按钮调用下面的这个函数()并传递参数,在我的例子中我传递了新的

    function checkforNew(){
                    //jQuery.post('<%=request.getContextPath()%>/InspectionController?&oper=new');
    
                    jQuery.ajax({
                        type: "POST",
                        url: '<%=request.getContextPath()%>/MyController?&oper=new',
                        //data: {'obj':data},
                        dataType: "json",
                        success: function(response){
                            window.location.href = response.redirect;
                        }
                    });
                }
    

    在您的 servlet 中,您需要提及以下代码,以便页面重定向。

    String destination = "/mypage.jsp;
    
                    response.setContentType("application/json");
                    JSONObject responsedata = new JSONObject();
                    responsedata.put("redirect", destination);
    
                    out.print(responsedata);
                    out.flush();
    

    【讨论】:

    • 发现它不起作用,但是在ajax之后它与回调函数一起工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多