我有一个使用ajax的脚本向服务器发送POST请求。服务器写在烧瓶中。当我提交表单时,我只得到一个带有文本的浏览器页面:
无法获取/login?email=my_email%40email.com&password=password&patient=n
因为它说不能获取我假设它是导致问题的表单子目录,而不是实际发送到服务器。我还用python测试程序测试了后端(rest api),它的工作原理我不会发布那部分。如果您认为它对决议有意义,我可以发布它。下面是脚本和html表单。
脚本:
$(document).ready(function() {
//set what happens when the "Enter" button is pressed
$('form#login-form').submit(function(event) {
login(event);
});
function login(event) {
var email = $("input[name='email']").val();
var password = $("input[name='password']").val();
var patient = $("input[name='patient']:checked").val();
var anorapp = '';
var deviceid = '';
if (userAgent.match(/Android/i))
anorapp = 'android';
else
anorapp = 'ios';
var json = {
patient: patient,
email: email,
password: password,
deviceid: deviceid,
anorapp: anorapp
};
$.ajax({
type: 'POST',
url: "http://127.0.0.1:5000/rest_api/v1.0/signin",
data: JSON.stringify(json),
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function(data, status) {
window.location.assign('map.html');
},
statusCode: {
404: function() {
alert('This account probably doesn\'t exist.');
},
403: function() {
alert('You have may inserted the wrong password.');
},
}
});
//avoid form submission (the default action of the event)
event.preventDefault();
}
});
HTML表单:
<div class="w-form">
<form id="login-form" name="email-form" data-name="Email Form" data-redirect="map.html" action="/login">
<div>
<label class="label-form" for="email-field">E-MAIL</label>
<input class="w-input input-form" id="email-field" type="email" name="email" data-name="email" required="required">
<div class="separator-fields"></div>
</div>
<div>
<label class="label-form" for="email">PASSWORD</label>
<div class="w-clearfix block-input-combined">
<input class="w-input input-form left" id="password-field" type="password" name="password" data-name="password" required="required"><a class="right-input-link" href="forgot.html" data-load="1">Forgot Password</a>
</div>
<div class="separator-button-input"></div>
</div>
<div>
<label class="label-form middle" for="email">PATIENT?</label>
<div class="w-clearfix input-form one-line">
<div class="w-clearfix radios-container">
<div class="w-radio w-clearfix radio-button">
<div class="radio-bullet-replacement"></div>
<input class="w-radio-input radio-bullet" id="patient-n" type="radio" name="patient" value="n" data-name="patient">
<label class="w-form-label" for="patient-n">No</label>
</div>
<div class="w-radio w-clearfix radio-button">
<div class="radio-bullet-replacement"></div>
<input class="w-radio-input radio-bullet" id="patient-y" type="radio" name="patient" value="y" data-name="patient">
<label class="w-form-label" for="patient-y">Yes</label>
</div>
</div>
</div>
<div class="separator-fields"></div>
</div>
<input class="w-button action-button" type="submit" value="Sign In" data-wait="Please wait...">
<div class="separator-button"></div><a class="link-upper" href="signup.html" data-load="1">YOU DON’T HAVE AN ACCOUNT? <strong class="b-link">SIGN UP</strong></a>
</form>
<div class="w-form-done">
<p>Thank you! Your submission has been received!</p>
</div>
<div class="w-form-fail">
<p>Oops! Something went wrong while submitting the form</p>
</div>
</div>