我正在與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中發佈發佈請求後重定向?
如果你想重定向,你爲什麼要用Ajax做它?爲什麼不以正常的方式提交表單?使用Ajax的主要原因是避免重定向。 –
我使用ajax來動態改變頁面的數據,併發送複雜的數據到服務器......我不是唯一的! ;) –
python代碼是不太可讀的,因爲有一些錯誤的身份... – acidjunk