Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

python - AJAX POST request with Flask returns method not allowed error

I recently started to learn Flask. I am trying to build a simple registration form, but I am getting a Method not allowed error. I spent some time reading solutions here and on google but without any success. I hope someone could help me to resolve the problem.

      <form method="POST" id="registerform">
        <div class="form-group">
          <small><label for="email">Email address</label></small>
          <input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email">
        </div>
        <div class="form-group">
          <label for="password">Password</label>
          <input type="password" class="form-control" id="password" placeholder="Password">
        </div>
        <div class="form-group">
          <label for="password2">Repeat Password</label>
          <input type="password" class="form-control" id="password2" placeholder="Repeat Password">
        </div>
        <button type="submit" class="btn btn-primary">Register</button>
      </form>
    </div>
    <div class="col"></div>
  </div>
</div>
$(document).ready(function() {
  $("#registerform").on('submit', function(e) {
    e.preventDefault();
    
    $.ajax({
      method: "POST",
      url: "/register",
      dataType: "html",
      data: $("#registerform").serialize(),
      success: function(data) {
        if (data == 'Registration succesful, please check your mail to activate account') {
          $('#msg').html('<div class="alert alert-success" role="alert" id="msg2">' + data + '</div>');
        } else {
          $('#msg').html('<div class="alert alert-success" role="alert" id="msg2">' + data + '</div>');
        }
      }
    });
  });
});
@app.route('/register', methods =['GET', 'POST']) 
def register(): 
    msg = '' 
    if request.method == 'POST' and 'password' in request.form and 'email' in request.form and 'password2' in request.form: 
        password = request.form['password']
        password2 = request.form['password2'] 
        email = request.form['email'] 

        #SQL QUEIRES AND OTHER STUFFS HERE#

        msg = 'Registration succesful, please check your mail to activate account.'
    else: 
        msg = 'Please fill out the form !'
    
    return msg

Thank you in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...