Reputation: 3559
I want to redirect user to another route if the code matches depending upon the values returned in the ajax success. Its working great now How do I write the script to redirect to the next route like redirect in php.
AJAX to check the code matches and its working great. What next
<script>
$("#codeModal form").submit(function(event) {
event.preventDefault();
var codetyped=$(this).find(".codeinput").val();
$.ajax({
type:"post",
url:"{{request.route_url('testingajax')}}",
data:{'codetyped':codetyped},
success:function(res){
alert(res);
}
});
});
</script>
Here is the view config
@view_config(route_name='testingajax', renderer='json')
def hello(request):
# return request.session['secretcode']
# return request.params.get('codetyped')
if int(request.params.get('codetyped')) == request.session['secretcode']:
return 'Success'
else:
return 'Error'
Upvotes: 0
Views: 538
Reputation: 1930
I'm not into Python, but it seems you are returning either 'Success' or 'Error' to your Ajax call.
Within your Ajax success callback function, you could simply check the returned value and redirect from there.
success:function(res){
// Test if there is a returned value in the res variable
if(typeof res !== 'undefined'){
// Check the res variable from a few options
switch(res){
case 'Success':
window.location.href='/some-success-page.html';
break;
case 'Error':
window.location.href='/some-error-page.html';
break;
}
}
}
Upvotes: 3