【发布时间】:2018-03-09 13:37:26
【问题描述】:
我想使用 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>
这将是控制器上的 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'});
【问题讨论】: