wx.request
1.wx.request相當於ajax請求,和django后台進行交互
官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html
參數 Object object
object.method的合法值
object.dataType的合法值
object.success回調函數
參數 Object res
示例代碼
在test.wxml設置一個按鈕
<button bindtap='click'>按鈕1</button> #點擊按鈕觸發click事件
在頁面js中
click:function(){ wx.request({ url: 'http://127.0.0.1:8000/app01/test/', #訪問django后台路徑 data:{'name':'jason'}, #往后台傳值 method:"POST", #POST提交方式,后台也應該是POST函數 header:{"content-type":"application/json"}, #請求頭header success:function(res){ #res是接收后台返回給前台的數據 console.log(res) console.log(res.data) } }) }
console.log打印后台傳遞數據
django后台views視圖代碼
from rest_framework.views import APIView from rest_framework.response import Response class Test(APIView): def post(self,request): print(request.data) #打印前台傳遞過來的數據 {'name': 'jason'} return Response({'code':'aaa'}) #返回給前端的數據