ajax调用的flask端点

时间:2017-08-24 17:40:51

标签: flask jinja2

我在按下提交按钮的同时对我的服务器进行了ajax调用。 按钮出现在页面http://127.0.0.1:5000/post/15中。

分别在python中调用Jquery函数和submit_comment的端点定义:

function submit_comment(post_id) {
    var uname = $('input[name="uname"]').val();
    var comment = $('textarea[name="comment"]').val();

    $.ajax({
        url: "/submit_comment",
        data: {name: uname, comment: comment, post_id: post_id},
        method: "POST",
        datatype: 'json',
        success: function(response) {
            console.log('reaches here');            
            addElement(response);
            console.log('Is it me');
        },
        error: function(error) {
            console.log('reached Error');
            console.log(error);
        }
    });
}

吡啶

@main.route('/submit_comment', methods = ['POST', 'PUT'])
def submit_comment():
    entry = request.get_json(force=True)
    print 'comment:', entry
    ....

主要是蓝图。但是我得到了404错误。

127.0.0.1 - - [24 / Aug / 2017 10:30:55]" POST / post / submit_comment HTTP / 1.1" 404 -

我想知道这篇帖子被附加到端点的位置。 有人可以帮助我理解错误并解决它吗?

谢谢, 迪帕克

1 个答案:

答案 0 :(得分:0)

我不太了解,但我知道ajax上的Flask文档说要像这样查询脚本根目录:

$SCRIPT_ROOT = {{ request.script_root|tojson }};

然后将其用作请求网址的一部分。

我使用的ajax调用示例($.getJSON$.ajax的jquery函数)

$.getJSON($SCRIPT_ROOT + '{{ url_for("help_email") }}',