2011-07-08 107 views
6

我正在与Django合作。我有一个HTML页面,在那里我做一些JavaScript的东西,然后我做了一个jQuery后,以这样的方式POST请求与jquery后重定向

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

这个帖子的请求后,我的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,但结果相同。

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

+0

如果你想重定向,你为什么要用Ajax做它?为什么不以正常的方式提交表单?使用Ajax的主要原因是避免重定向。 –

+0

我使用ajax来动态改变页面的数据,并发送复杂的数据到服务器......我不是唯一的! ;) –

+1

python代码是不太可读的,因为有一些错误的身份... – acidjunk

回答

8

好吧,我已经找到了神奇解! :)

的情况: 有)

由jQUery.ajax(称为视图的方法Ajax的重定向后,我用的提示找到here(使用的文档,而不是“体”中的jQuery selecotor)

但是,我也有进一步的操作要做。

在所谓的视图

,调用另一个方法,像这样:

[...] 
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)) 

希望这可以成为有用的人! ;)

1

@Daniel是要避免使用Ajax调用重定向是正确的,但你可能看看这个堆栈溢出问题,其中涉及如何做到这一点:How to manage a redirect request after a jQuery Ajax call

+0

这就像我的解决方案,我认为..在你的链接我没有找到有用的信息对我来说..有没有关于发送数据通过js后发送数据或通过后python重定向后的信息! :( –

1

在点击链接调用下面这个函数(),并传递参数时,在我的情况下,我通过了新的

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(); 
+0

发现它不起作用,但它与ajax后的回调函数一起使用 – Sinux