2011-07-08 106 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