Я пытаюсь вызвать метод бэкэнд-фляги Python с помощью события onclick кнопки (getotp) с помощью метода getOTP () из javascript.
Не могли бы вы подсказать мне, как сделать вызов ajax / jquery для вызовакод Python внутреннего интерфейса, который отправляет электронное письмо пользователю
function getOTP() {
document.getElementById("div1").innerHTML = "✔ OTP has been sent to your email id, Please enter and click login button";
document.getElementById("div1").style.color = "Green";
document.getElementById("OTP").disabled = false
document.loginform.OTP.focus();
}
function ValidateEmail(email_id) {
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (email_id.value.match(mailformat)) {
document.getElementById("div1").innerHTML = "✔ Its a valid email address";
document.getElementById("div1").style.color = "Green";
document.getElementById("getotp").disabled = false
//document.loginform.OTP.focus();
return true;
} else {
document.getElementById("div1").innerHTML = "Enter a valid Email ID., eg: example@xyz.com";
document.getElementById("div1").style.color = "Red";
document.loginform.email_id.focus();
return false;
}
}
<div class="form-group">
<input type="text" class="form-control" name="email_id" id="email_id" placeholder="Email" required="required" onfocusout="ValidateEmail(email_id)">
<div id="div1"></div>
<div id="div3"></div>
<button type="button" id="getotp" name="getotp" onclick="getOTP()" class="btn btn-primary btn-xs btn-block ">Get OTP</button>
</div>
<div class="form-group">
<input type="password" class="form-control" id="OTP" name="OTP" placeholder="Enter your OTP here.." required="required" onblur="validateOTP(OTP)">
<div id="div2"></div>
</div>
def OTPGenerator():
digits = "0123456789"
newotp = ""
for i in range(6):
newotp += digits[math.floor(random.random() * 10)]
return newotp
@app.route('/SendMail', methods=['GET','POST'])
def SendMail():
try:
email = request.form['email_id']
otpnow = OTPGenerator()
msg = Message("OTP for Electric Man Login", sender = "example@gmail.com", recipients = [email])
msg.body = "Hi, \n OTP for Electric Man Login is " + otpnow
if (mail.send(msg)):
return jsonify(status="success")
else:
return jsonify(status="failure")
except:
print("Mail Exception")