<分区> 分区>
我想使用 Ajax 调用服务器端函数。
我找到了一个简单的 PHP 示例 in this post .我认为如果我们可以包含这个完全相同的示例但用于 Python/Flask MVC 框架,社区将会有所改善。
这是View端的ajax代码,叫做test.html:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
function create () {
$.ajax({
url:"test1", //the page containing python script
type: "post", //request type,
dataType: 'json',
data: {registration: "success", name: "xyz", email: "abc@gmail.com"},
success:function(result){
console.log(result.abc);
}
});
}
</script>
这将是 Controller 上的 Python 代码:
@app.route('/test', methods=['GET','POST'])
def test():
return render_template("test.html", brand = brand)
@app.route('/test1', methods=['GET','POST'])
def test1():
if registration == "success":
return json.dump({"abc":'successfuly registered'});
