1

I'm just doing some simple request from ajax to Flask, but it doesn't work.

Here is my basic.html vs js/basic.js code:

$(document).ready(function(){
  $('form').on('submit', function(e) {
    $.ajax(type: 'GET', url: '/home/check').done(function(data){
      $('#status_id').text("Submitted");
    });
    e.preventDefault();
  });
});
{% extends 'layout.html' %}

{% block main %}
<script src="{{ url_for('static', filename='js/basic.js') }}" charset="utf-8"></script>
<h1 id="status_id"></h1>
<form>
  <div class="form-group">
    <input type="text" class="form-control" name="url" id = "urlInput" aria-describedby="urlHelp" placeholder="">
    <small id="urlHelp" class="form-text text-muted">Enter URL or some message to generate your QR code.</small>
  </div>
  <button type="submit" class="btn btn-primary">Generate</button>
</form>
<img src="/static/images/new.png" alt="There is no image!">
{% endblock %}

Here is my python code, I've already register a blueprint called bp_home:

bp_home = Blueprint('home', __name__, url_prefix='/home')
@bp_home.route('/check')
def check():
    return jsonify({"isValid": "True"})

@bp_home.route('/basic_generator')
def basic_generator():
    return render_template('home/basic.html')

The above code doesn't work and when i submit the form, it automaticly redirect to GET method like this: http://127.0.0.1:5000/home/basic_generator?url= any suggest ??

3
  • Does basic_generator appear elsewhere in your code? If so, show us that bit of code. Commented Mar 21, 2019 at 5:16
  • I've edited, but I don't think it influences what I'm getting Commented Mar 21, 2019 at 5:22
  • is it because the absolute path in my script? Commented Mar 21, 2019 at 5:30

1 Answer 1

2

I think I've solved it. The problem was that I'm using jquery.slim.min cdn version copying from bootstrap docs, which miss a lot of functionalities for ajax. Change it into jquery.min version, everything would be OK.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.