【问题标题】:How to send html data to flask without form如何在没有表单的情况下将 html 数据发送到烧瓶
【发布时间】:2019-02-17 13:17:48
【问题描述】:

我知道您可以使用 jquery 中的 $.ajax() 之类的调用轻松地将表单数据发送到烧瓶,但我需要的是一种使用元素(例如 <p> 标记)将数据发送到烧瓶服务器的方法。

例如: 测试.html

<p data-rep='testentry' class='qtemp'>Entry00</p>
<p data-rep='testentry01' class='qtemp'>Entry01</p>

index.js

$(document).ready(function(){
    $('.qtemp').on('click',function(){
        var layout = $(this).data('rep');

        $.ajax({
            url: 'workstation',
            type: 'POST',
            data: layout
        });     
    });
});

main.py

@app.route('/workstation',methods=['GET','POST'])
def workstation(data):
    layout = data

    return render_template('station.html',target=layout)

运行这个应用程序:

  1. 不渲染模板station.html
  2. 不打印包含已发送数据的layout 变量(我添加了一个打印语句但它不起作用,甚至尝试将其写入文件)

我尝试过的:

  1. index.js 中,将data: layout 替换为data: JSON.stringify(layout),然后在.main.py 中,将layout = data 替换为layout = request.args.get('data')

不用说,这一切都行不通

注意:不能使用 html 表单

【问题讨论】:

  • 您的 Ajax 请求中没有 success 回调,因此响应会被忽略

标签: python jquery python-3.x flask


【解决方案1】:

您需要修改您的 ajax 以确保您从 Python 路由接收到 JSONified 结果。此外,为了存储从 ajax 调用返回的数据,您必须使用 flask.request.args 按键名访问值:

index.js:

$(document).ready(function(){
   $('.qtemp').on('click',function(){
    var layout = $(this).data('rep');
     $.ajax({
      url: "/workstation",
      type: "get",
      data: {layout: layout},
      success: function(response) {
        var new_html = response.html;
      },
     });     
  });
});

main.py:

@app.route('/workstation')
def workstation():
  layout = flask.request.args.get('layout')
  return flask.jsonify({'html':flask.render_template('station.html',target=layout)})

编辑:您可以将从ajax 请求中获得的值存储在flask.session 中,并重定向到所需的页面。为此,创建一个额外的路由来保存该值,然后在 ajax success 函数的主体中使用 window.location.replace

index.js:

$(document).ready(function(){
  $('.qtemp').on('click',function(){
    var layout = $(this).data('rep');
    $.ajax({
      url: "/update_layout",
      type: "get",
      data: {layout: layout},
      success: function(response) {
        window.location.replace('/workstation');
      },
    });     
  });
});

main.py

import string, random

app.secret_key = ''.join(random.choice(string.printable) for _ in range(20))
#secret_key needed for session implementation

@app.route("/update_layout")
def update_layout():
  flask.session['layout'] = flask.request.args.get('layout')
  return flask.jsonify({'success':'True'})

@app.route('/workstation', methods=['GET'])
def workstation():
  return flask.render_template('station.html',target = flask.session['layout'])

【讨论】:

  • 我想要的是将带有布局变量的页面呈现为 ptag 上的文本,但这不是这样做的,这会将 HTML 内容(代码)发回给我
  • @silverhash ajax 请求不用作重定向,而是仅将数据返回到发送请求的当前页面(因此为“动态”)。为了实现伪形式的帖子重定向,我添加了一个解决方案,其中flask.session 和一个额外的路由作为一种可以跨请求保存数据的方法。在success 函数的主体中,使用了重定向。
  • 我用 get 请求替换了我的 post 请求,并将我的数据转换为一个对象,就像你现在所做的那样,数据实际上最终发送到了 flask ..但总结一下,模板是仍然没有被渲染
  • @silverhash 点击后页面是否被重定向到/workstation
  • 是的,它是..您的编辑..我现在可以成功重定向,但布局变量似乎没有发送到 station.HTML 页面,现在问题可能出在我这边我猜..另一件事,是正确的方法还是我们在这里偷偷摸摸:D
猜你喜欢
  • 2017-12-26
  • 1970-01-01
  • 2023-03-11
  • 2020-06-01
  • 2019-12-22
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
相关资源
最近更新 更多