小程序向django后端交互
目录
小程序向django后端交互
1.前后端数据交互
- 基本流程:
wx.request()==>django(url路由)==>django(调用视图函数)==>sqlite3取数据==>返回Json格式数据
1.1前端获取后端传来的数据
bug1:后端获取不到前端传来的值?
回答:使用ajax千万不要使用 jquery 来找ID ,因为一个网页中ID是唯一的。
wx.request()wx.request({ url:'https://域名ID/index', //必填,其他的都可以不填 data:{ a:1, b:2 }, header:{ 'content-type':'application/json' }, method:'GET', dataType:'JSON', responseType:'text', success(res){ //接口调用成功回调函数 console.log(res); }, fail(){ //调用失败:回调函数 console.log('fail') }, complete(){ console.log('complete') } )可以类比
jquery中的Ajax请求格式理解django路由关于创建
django项目方法不在给出,后端用django或者springboot都可以。
调用视图函数,取数据,返回数据
返回的是
Json,对象类型与字符串类型转换。
显示数据
onLoad(options) { wx.request({ url: 'http://127.0.0.1:8000/orders/', header: { 'content-type': 'application/json' }, method: 'GET', dataType: 'JSON', responseType: 'text', success(res) { console.log(res.data); }, }) },

要注意的:咱们这里请求网址没有证书,所以要把小程序中不校验合法域名给勾上。
至此,前端获取后端数据成功。
1.2 后端获取前端传来的数据
小程序中这样写

django
中这样写



