How does ajax work with python? [closed]

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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) 

回答1:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!