python - 为什么我的 ajax 代码不能使用 Python Flask 运行?

标签 python jquery ajax flask

我只是从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/

相关文章:

python - 将列表与列表列表进行比较,并从列表列表中返回另一个元素

javascript - 在我的例子中,on() 在绑定(bind)函数中不起作用

JavaScript:全局变量代码示例 "Use Strict"说明

javascript - 使用 AJAX 将 cookie session 数据存储到 Jquery(不使用 PHP)

javascript - 处理 Ajax JSON 数据时,如何在 .each() 循环中使用 .html() 中的变量?

javascript - 如何从递归请求返回 promise 并在数据匹配条件时解决它?

Python 3.4 和 3.5 - JsonDecodeError 兼容性

python - 在cython中编译时如何删除python断言?

python - Fudge mock datetime.now

javascript - 跨浏览器启用/禁用鼠标滚轮