将json格式的数据从网页传递到flask应用程序

2024-04-24 17:25:22 发布

您现在位置:Python中文网/ 问答频道 /正文

  • 我想使用ajaxpost方法将json格式的数据从网页发送到flask。我的尝试如下:

mytry:mytest.html(代码段)

var jsondat = JSON.stringify(table.getData());
console.log(jsondat);
$.ajax({
    type: 'POST',
    url: '127.0.0.1:5000/test',
    // contentType: 'application/json;charset=UTF-8',
    data: {
        'data': jsondat
    },
    success: function(response) {
        alert("HELLO")
    },
});

烧瓶应用程序:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/<string:page_name>/')
def render_static(page_name):
    return render_template('%s.html' % mytest)


@app.route('/test')
def testfun():
    content = request.get_json(silent=True)
    print(content)
    return 'The test'

if __name__ == '__main__':
    app.run()

Tags: nametestjsonappflaskdatadefhtml