用Flask很简单的:比如:
index.html 放到templates文件夹下:
$(function() {
$('#calculate').click(function(){
$.ajax({
url: '/addnumber',
data:{
a: $('#a').val(),
b: $('#b').val()
},
dataType: 'JSON',
type: 'GET',
success: function(data){
$("#result").html(data.result);
}
});
});
});
addNum
+
=
?
calculate
python脚本run.py:
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
@app.route('/')
@app.route('/index.html')
def index():
return render_template('index.html')
@app.route('/addnumber')
def add():
a = request.args.get('a', 0, type=float)
b = request.args.get('b', 0, type=float)
return jsonify(result=a + b)
if __name__ == "__main__":
app.run()
安装好了flask之后,直接python run.py即可