POST请求与jQuery后重定向 [英] Redirect after POST request with jquery

查看:396
本文介绍了POST请求与jQuery后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python的Django的工作。 我有一个HTML页面,在那里我做SOMO JS的东西,然后,我做一个jQuery的岗位,以这种方式:

I'm working with python on django. I've an html page, where i do somo js stuff, and then, i do a jquery post, in this way:

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

这个帖子以后,我的Python的观点intercpet正确调用此URL。 我应该做的是处理数据和发送该用户在其他网页,而发送到destitnation页面此数据。 现在的问题是,我不能做到像往常一样重定向的蟒蛇,它像code忽略重定向行。

After this post, my python's view intercpet correctly the call for this url. What i should do is process the data and send the user in an other page, while sending to the destitnation page this data. The problem is that i can not do the redirect like usual in python, it's like the code ignores the redirect row.

我的Python的code是:

My python's code is:

@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)) 

我已经找到了为改变页面的唯一方法是:

The only way i've found for change page is:

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

(写入后,code写了) 但这种方式我不知道怎么传的蟒蛇,我需要的数据。

(written after the code written up) but in this way i don't know how to pass at python the data i need

在HTML code:

The html code:

<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> 

PS。我试过也$。员额,但具有相同的结果。

ps. i've tried also $.post, but with the same results

我怎样才能做一个重定向afeter在Django做jquery的帖子?

How can i do a redirect afeter a post made with jquery in django?

推荐答案

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

Ok, I've found the magical solution! :)

情况: 有称为jQUery.ajax视图的方法()

The situation: there is a view's method called by jQUery.ajax()

要阿贾克斯后重定向,我用发现的此处(使用的文档,而不是在jQuery的selecotor'身体')

To redirect after Ajax, I've used the tips found here (use document instead of 'body' in the jQuery selecotor)

不过,我也有进一步行动的事情。

But, I've had also further operations to do.

在所谓的观点,调用另一个方法,像这样的:

in the called view, call another method, like this:

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

我救,我需要在会话中的数据。我在叫视图中使用这些数据。

I save the data that I need in the session. I use this data in the called view.

调用另一方法它的键为重定向浏览器的页面。 事实上,在调用方法结束后,就可以正常书写和放大器;执行:

Call another method it's the key for redirect the page of the browser. In fact, at the end of the called method, you can normally write&execute:

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

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

Hope this can be useful for someone! ;)

这篇关于POST请求与jQuery后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆