如何用Python做AJAX的工作? [英] How does ajax work with python?

查看:128
本文介绍了如何用Python做AJAX的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被周围的Googling,但我不太明白如何AJAX的作品。 能取悦别人解释它是如何工作?

  $。阿贾克斯({
        网址:{{url_for('。app.slideshow<标签>')}},
        类型: ,
        数据:{'参数':},
        数据类型: ,
        成功:函数(响应)
        {
        }
 

我想要做的就是看是否document.getElementsByClassName(电流)发生了变化。如果有,它会要求app.py对当前注释和标记,并且无需刷新更新页面。我不知道写什么,要么接受这个就app.py。

我会包括我的app.py,但它是不好的。

 从瓶中瓶进口,会话,url_for,请求重定向,render_template
进口API,数据库
应用=瓶(__ name__)
#app.secret_key =秘密

@ app.route('/幻灯片/<标签>',方法=GET,POST])
高清幻灯片():
如果request.method ==GET:
    PIC = request.GET中(当前)。HREF
    标记列表= db.getTaglist()
    标签= db.getTags(图)
    piclist = db.getPics(小于标签>)
    commentlist = db.getComments(图)
    返回render_template(slide.html标记列表=标记列表,标签=标签,piclist = piclist,commentlist = commentlist,URL = URL)
其他:
    按钮=的Request.Form [按钮]
    PIC = request.GET中(当前)。HREF

    如果按钮==提交:
        aComment =的request.form ['评论']
        db.addComment(PIC,aComment)
    ELIF键==submitnewtag:
        如果的request.form ['Addnewtag']
            ATAG =的request.form ['Addnewtag']
            db.addTag(PIC,ATAG)
        其他:
            ATAG =的request.form ['选择一']
            db.addTag(PIC,ATAG)


如果__name __ ==__ main__:
    app.debug = TRUE
    app.run(端口= 5300)
 

解决方案

通常情况下,你的服务器上的AJAX处理程序应返回XML或JSON(我认为JSON是更好)与它所需要的数据。

因此​​,获得在hanler信息,达姆它JSON并返回到客户端。在

在客户端,使用Javascript收到此JSON,之后应该动态地创建HTML元素和网页正文中插入他们。

通过探索这个简单的教程由瓶的创建者的开始。

I'm been googling around, but I don't quite understand how ajax works. Could please somebody explain how this works?

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

What I'm trying to do is see if document.getElementsByClassName(current) has changed. If it has, it will ask app.py for comments and tags on current, and update the page without refreshing. I have no idea what to write to receive this on the app.py either.

I'll include my app.py, but it is not good.

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)

解决方案

Usually, ajax handler on your server should return XML or JSON (I think JSON is better) with the data it needs.

So, after getting info in hanler, dum it to JSON and return to client.

On client, Javascript receives this JSON, and after that should dynamically create html elements and insert them in your page body.

Start by exploring this simple tutorial by Flask's creator.

这篇关于如何用Python做AJAX的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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