Redirect after POST request with jquery

I work with Django. I have an HTML page where I use some Javascript material and then I make a jQuery message as follows:

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

After this mail request, my Django view correctly handles the call for this URL. I want him to process the data, send the user to another page and send this data to a new page. The problem is that I cannot perform the redirection, as usual, in Python, as the code ignores the redirection.

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 found changing the page is through Javascript after the code above:

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

But I do not know how to transfer the data that I need when using this method.

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 I also tried $.post , but with the same results.

How can I do a redirect after a mail request made using jQuery in Django?

+6
source share
3 answers

Ok, I found a magic solution! :)

Situation: There is a view method called jquery.ajax ()

To redirect after Ajax, I used the tips found here (use the document instead of โ€œbodyโ€ in the jQuery selector)

But I also had further operations.

in the called view, call another method, for example:

 [...] 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 a session. I use this data in the called view.

Call another method - this is the key to redirect the browser page. In fact, at the end of the called method, you can usually write & execute:

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

Hope this can be helpful for someone !;)

+8
source

@Daniel is correct in that you want to avoid redirecting using ajax calls, but you can take a look at this question that discusses how to do this: How to manage redirecting a request after jQuery Ajax call

+1
source

On the button, click this function () below and pass the parameter, in my case I passed a new

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

Inside your servlet, you need to specify the following code to redirect the page.

 String destination = "/mypage.jsp; response.setContentType("application/json"); JSONObject responsedata = new JSONObject(); responsedata.put("redirect", destination); out.print(responsedata); out.flush(); 
+1
source

Source: https://habr.com/ru/post/892320/


All Articles