wx.request 請求


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'})  #返回給前端的數據


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM