【问题标题】:How does ajax work with python? [closed]ajax 如何与 python 一起工作? [关闭]
【发布时间】:2012-11-20 03:23:10
【问题描述】:

我一直在谷歌搜索,但我不太明白 ajax 是如何工作的。 有人能解释一下这是如何工作的吗?

$.ajax({
        url: "{{ url_for( 'app.slideshow.<tag>' ) }}",
        type: "",
        data: {'param':},
        dataType: "",
        success : function(response)
        {
        }

我要做的是查看 document.getElementsByClassName(current) 是否已更改。如果有,它将向 app.py 询问当前的 cmets 和标签,并在不刷新的情况下更新页面。我也不知道在 app.py 上要写什么来接收这个。

我将包含我的 app.py,但它并不好。

from flask import Flask,session,url_for,request,redirect,render_template
import api,db   
app = Flask(__name__)
#app.secret_key = "secret"

@app.route('/slideshow/<tag>', methods=['GET', 'POST'])
def slide():
if request.method=="GET":
    pic = request.get('current').href
    taglist = db.getTaglist()
    tags = db.getTags(pic)
    piclist = db.getPics(<tag>)
    commentlist = db.getComments(pic)
    return render_template("slide.html", taglist = taglist, tags =tags, piclist =piclist, commentlist = commentlist, url = url)
else:   
    button = request.form['button']
    pic = request.get('current').href

    if button=="submit":
        aComment = request.form['comment']
        db.addComment(pic,aComment)
    elif button == "submitnewtag":
        if request.form['Addnewtag']
            aTag = request.form['Addnewtag']
            db.addTag(pic,aTag)
        else:
            aTag =  request.form['select1']
            db.addTag(pic,aTag)


if __name__=="__main__":
    app.debug=True
    app.run(port=5300)

【问题讨论】:

  • 这似乎不是一个问题。你到底在问什么,它与 Python 有什么关系?

标签: python ajax flask


【解决方案1】:

通常,您服务器上的 ajax 处理程序应该返回 XML 或 JSON(我认为 JSON 更好)以及它需要的数据。

因此,在从处理程序获取信息后,将其转储(转换)为 JSON 对象并返回给客户端。

在客户端,JavaScript 接收此 JSON,然后动态创建 html 元素并将它们插入到您的页面正文中。

首先探索 Flask 的创建者 this simple tutorial

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2011-10-13
    • 2011-04-04
    相关资源
    最近更新 更多