How does ajax work with python?

ajax, flask, python

Solution

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 from the handler, dump(cast) it into a JSON object 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.

Problem

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) ```

Original source