我正在与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