0
Ajaxを使用してサーバー側の関数を呼び出したいとします。
PHP in this postの簡単な例が見つかりました。私は、この同じ例をPython/Flask MVCフレームワークに含めることができれば、コミュニティが改善すると思います。
これはtest.htmlという呼ばれるビュー側のAjaxコードです:
<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: "[email protected]"},
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'});