使用flask开启一个web服务 ./vtest1.py

from flask importFlask, render_templateimportpickleimportjson

app= Flask(__name__)

@app.route(‘/‘)defindex():

context={‘homename‘ : ‘这是首页‘}return render_template(‘index.html‘, **context)

@app.route(‘/homeapi‘)defindexapi():

data={‘name‘ : "lili",‘age‘:29,‘degree‘ : ‘benke‘}returnjson.dumps(data)if __name__ == "__main__":

app.run()

./templates/index.html模板文件如下:

Title

{{homename}}

单选1:单选2:


name:name:age:degree:

var vm=new Vue({

el :‘#app‘,

data : {

name:‘uuuu‘,

age:0,

degree:‘‘,

select:‘checked‘,

},

mounted : function () {

$.ajax({

url:‘/homeapi‘,

type:‘GET‘,

dataType:‘json‘,

beforeSend:function(){//请求前的处理

},

success:function(req){//请求成功时处理

console.log(req)

console.log(req.name)

vm.name=req.name;

vm.age=req.age;

vm.degree=req.degree;

},

complete:function(){

},

error:function(){//请求出错处理

console.log(‘error‘)

}

});

}

})

开启flask服务

* Serving Flask app "vtest1" (lazy loading)

* Environment: production

WARNING: This is a development server. Do not use it in a production deployment.

Use a production WSGI server instead.

* Debug mode: off

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

127.0.0.1 - - [23/Jun/2020 18:43:11] "GET / HTTP/1.1" 200 -

127.0.0.1 - - [23/Jun/2020 18:43:11] "GET /homeapi HTTP/1.1" 200 -

访问效果如下:

1d5d52d56e755b605d5a04ef1ae28c60.png

原文:https://www.cnblogs.com/harryTree/p/13183840.html

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐