使用jquery重新定向POST请求 [英] Redirect after POST request with jquery

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

问题描述

我正在和Django合作。我有一个HTML页面,在那里我做一些Javascript的东西,然后我做一个jQuery的帖子,这样:

  $ 。$(
url:'/ xenopatients / measure / 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
printenter
if request.method =='POST':
print request.POST

if objin 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 =dataTablewidth =100%border =1>< / table>< br>
< script language =javascript>
document.measureForm.id_barcode.focus();
document.measureForm.Add.disabled = false;
$('#dataTable')。tablePagination({});
< / script>
< input type =buttonname =savevalue =Save Measure Serieonclick =table2JSON('dataTable')/>
< / form>

我也尝试过 $。post ,但结果相同。



在Django中使用jQuery发布请求?

解决方案

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



情况:
有一个视图的方法,由jQUery.ajax()调用$ /

>要在Ajax之后重定向,我使用了这里(在jQuery selecotor中使用文档而不是body)



但是,我还有进一步的操作。



在调用视图中,调用另一种方法,如下所示:

  。] 
如果request.method =='POST':
如果request.POST中的obj:#ricevuti dati da ajax
request.session ['qual'] = request.POST ['obj']
return HttpResponseRedirect(reverse(xenopatients.views.qualReport))

我保存会话中需要的数据。我在调用视图中使用这些数据。



调用另一种方法是浏览器页面重定向的关键。
事实上,在被调用的方法的结尾,你通常可以写& execute:

  return render_to_response(' measure / qualReport.html',{'name':name,'list_report':report,'message':'Measures正确保存'},RequestContext(request))

希望这可以对某人有用! ;)


I'm working with Django. I have an HTML page, where I do some Javascript 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 
}); 

After this post request, my Django view correctly handles the call for this URL. What I want it to do is process the data, send the user to another page, and send this data to the new page. The problem is that I cannot perform the redirect like usual in Python, it's like the code ignores the redirect.

My Python 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 to change the page is through Javascript after the code above:

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

But I don't know how to pass the data I need when using this method.

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> 

P.S. I've also tried $.post, but with the same results.

How can I do a redirect after a post request made with jQuery in Django?

解决方案

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

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

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! ;)

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

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