如何从烧瓶中接收.js文件上的jsonify变量

问题描述 投票:0回答:1

我想将Flask应用中的字符串发送到.js文件(而不是HTML上的脚本)。我知道我需要使用Ajax'GET'类型,但是我不确定如何在.js文件和console.log数据上接收它。

@app.route('/',methods =['GET','POST'])
def index():
    req = json.dumps(request.get_json())

    if request.method == 'POST':
        result = getString(req) #Function outputs a string
        print('Received')
        return jsonify(result)
    else:
        print('Not Received')

    return render_template('index.html')

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

。js文件(需要帮助):

$.ajax({
    url: "/",
    type: 'GET',
    data: ???, #What should i put here
    success: function(data) {
        console.log(data);
    }
});
javascript python json ajax flask
1个回答
0
投票

请注意,以下代码段表示如果端点收到GET请求,则不会返回任何内容。

if request.method == 'POST':
    result = getString(req) #Function outputs a string
    print('Received')
    return jsonify(result)
else:
    # This is what will get called on a GET request
    print('Not Received')

解决此问题之后,除非要提供一些查询参数,否则根本不需要ajax端的data字段。

© www.soinside.com 2019 - 2024. All rights reserved.