I have a login page, when user write username and password, I want to pass this information to server(post) and if I have error i want to just show error message and if I have not error so I want to submit it, in this case always prevent form submit.
In the below code always prevent submit and get error message from server and show it:
login.js
$('#login-form').submit(function(e){
var message;
$.post('/login/', $(this).serialize(), function(data){
obj = JSON.parse(data);
message = obj.message;
alert(message);
});
e.preventDefault();
});
view.py
// if username and password is faild
message="login information is failed"
return HttpResponse(json.dumps({'message': message}))
//else with specific data
return render(request, 'front/member.html', data)
I want to do like this:
if(message) e.preventDefault();
// else submit
but first e.preventDefault(); execute and then message set from server what can i do for this problem?