可以使用jquery post flask吗? [英] can flask use with jquery post?

查看:157
本文介绍了可以使用jquery post flask吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Flask很新,在进一步研究之前,是否有可能使用flask和json从jquery到python函数执行post?我也不熟悉json,因此所有这些代码对我来说都显得非常复杂。



有关示例

  from flask import烧瓶,jsonify,render_template,请求
app =烧瓶(__ name__)

@ app.route('/ _ add_numbers')

如何确定替代 / _ add_numbers ?我无法真正找到一个好的网站来向我展示一个简单的教程,或者使用flask来执行 post 的步骤。

这是我目前的jQuery函数:
$ b $ $ $ $ $' (function)(){
$ .post(cameraservo2.py,{direction:left})。done(function(reply){
$('#camerapos' ).empty()。append(reply);
alert(left button clicked);});

});

我可以不使用json吗?

解决方案

$。post()不使用 JSON。它发送一个普通的POST请求,使用 application / x-www-form-urlencoded 作为内容类型。在Flask路径中,这意味着您发布的字段位于 request.form 对象中。如果您的视图返回HTML,您可以将其插入HTML文档中。



只需选择一个反映您想要的路径名称即可:

  @ app.route('/ turn_servo',methods = ['POST'])
def turn_servo_ajax():
direction = request .form ['direction']

#做一些方向值;它是一个字符串
,如果direction =='left':
#...
else:
#...

return'<格式(方向)

将伺服50度转到{}< / div> >然后在 $。post 中使用该路由名称:



$ p $ $('#left_button')。click(function(){
$ .post(/ turn_servo,{direction:left})。done(function(reply){
$(' ();
alert(left button click);});

});


I am very new to Flask and before i proceed further, is it possible to perform a "post" from jquery to python function using flask and json? I'm not familiar with json too and therefore all those codes seem very complicated to me.

For example:

from flask import Flask, jsonify, render_template, request
app = Flask(__name__)

@app.route('/_add_numbers')

how do I determine what to substitute /_add_numbers? I can't really find a good site to show me a simple tutorial or steps to do post with flask.

This is my current jQuery function:

$('#left_button').click(function(){
            $.post("cameraservo2.py", {direction:"left"}).done(function (reply) {
                $('#camerapos').empty().append(reply);
                alert("left button clicked");});

        });

Can I do this WITHOUT using the json??

解决方案

$.post() doesn't use JSON. It sends a regular POST request, with application/x-www-form-urlencoded as the content type. In a Flask route this means that the fields you posted are found in the request.form object. If your view returns HTML you can insert it into your HTML document.

Just pick a route name that reflects what you want to do:

@app.route('/turn_servo', methods=['POST'])
def turn_servo_ajax():
    direction = request.form['direction']

    # do something with the direction value; it is a string
    if direction == 'left':
        # ...
    else:
        # ...

    return '<div>Turned the servo 50 degrees to the {}</div>'.format(direction)

You then use that route name in your $.post:

$('#left_button').click(function(){
            $.post("/turn_servo", {direction:"left"}).done(function (reply) {
                $('#camerapos').empty().append(reply);
                alert("left button clicked");});

        });

这篇关于可以使用jquery post flask吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆