我只是从ajax到Flask做了一些简单的请求,但它不起作用。
这是我的 basic.html 与 js/basic.js 代码:
$(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 %}
这是我的 python 代码,我已经注册了一个名为 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')
上面的代码不起作用,当我提交表单时,它会自动重定向到 GET 方法,如下所示: http://127.0.0.1:5000/home/basic_generator?url= 有什么建议吗??
最佳答案
我想我已经解决了。问题是我正在使用从 bootstrap 文档复制的 jquery.slim.min cdn 版本,它缺少 ajax 的很多功能。改成jquery.min版本就一切OK了。
关于python - 为什么我的 ajax 代码不能使用 Python Flask 运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55274045/