Redirect after POST request with jquery
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.开发者_JAVA技巧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! ;)
@Daniel is correct in that you want to avoid redirects with ajax calls, but you might take a look at this Stack Overflow question which addresses how to do so: How to manage a redirect request after a jQuery Ajax call
On button click call this function() below and pass the parameter, in my case I passed 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 mention following code, for the page to redirect.
String destination = "/mypage.jsp;
response.setContentType("application/json");
JSONObject responsedata = new JSONObject();
responsedata.put("redirect", destination);
out.print(responsedata);
out.flush();
精彩评论