小程序請求Django后台及路由跳轉


小程序請求Django后台(wx.requests)

小程序:

# pages/test1/test1.wxml
<button bind:tap="req">點我</button>
    
# pages/test1/test1.js  
Page({
    req:function(){
    wx.request({
      url: 'http://127.0.0.1:8000/test',
      data:{"name":"zhang"}, // 向后端發送的數據,后端通過request.data拿到該數據
      method:"POST",
      header:{
        "content-type":'application/json'
      },
      success(res) {
        console.log(res)
      }
    })
  }
})
'''
回調的結果res:

{data: {…}, header: {…}, statusCode: 200, cookies: Array(0), errMsg: "request:ok"}
cookies: []
length: 0nv_length: (...)__proto__: Array(0)
data: code: 200
msg: "OK"
__proto__: Object
errMsg: "request:ok"
header: 
Allow: "POST, OPTIONS"
Content-Length: "23"
Content-Type: "application/json"
Date: "Sat, 15 Feb 2020 15:38:35 GMT"
Server: "WSGIServer/0.2 CPython/3.6.4
"Vary: "Accept, Cookie"
X-Frame-Options: "SAMEORIGIN"
__proto__: Object
statusCode: 200
__proto__: Object
'''

# urls.py
from django.conf.urls import url
from django.contrib import admin
from app01.views import Test

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'test',Test.as_view())
]

# views.py
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.
class Test(APIView):
    def post(self,request):
        print(request.data)  # 小程序傳來的數據data,封裝到了request中。 {'name': 'zhang'}
        return Response({
            "code":200,"msg":"OK"
        })

小程序路由的跳轉方式

wx.switchTab(object,object)

跳轉到 tabBar 頁面,並關閉其他所有非 tabBar 頁面

參數

object object

# app.json
  "tabBar": {
    "color": "#456",
    "selectedColor": "#444444",
    "backgroundColor": "#ffffff",
    "borderStyle": "white",
    "list": [
      {
        "pagePath": "pages/test1/test1",  # 只有在app.json的TabBar注冊的頁面才能使用wx.switchTab跳轉
        "text": "首頁",
        "iconPath": "images/icon1.png",
        "selectedIconPath": "images/icon1-active.png"
      },
      {
        "pagePath": "pages/test2/test2",
        "text": "test2",
        "iconPath": "images/icon2.png",
        "selectedIconPath": "images/icon2-active.png"
      }
    ]
  },
# pages/test1/test1.wxml

<button bind:tap="click">wx.switchTab</button>
# pages/test1/test1.js
Page({
    click:function(){
    wx.switchTab({
      url:"/pages/test2/test2" # 只有在app.json的TabBar注冊的頁面才能使用wx.switchTab跳轉
    })
  }
})

wx.reLaunch(Object object)

關閉所有頁面,即可跳轉到tabBar頁面,也可以跳轉到非tabBar頁面

參數

Object object

# pages/test1/test1.js

click:function(){
    // wx.switchTab({
    //   url:"/pages/test2/test2"
    // })
    var name='sb'
    wx.reLaunch({
      url: "/pages/test3/test3?name="+name,  # 只要有這個頁面就能跳轉,還可以攜帶參數時,在要跳轉頁面加載的時候,接收這個參數
    })
  }

# pages/test3/test3.js
Page({
      onLoad: function (options) {
    console.log(options)  # {name: "sb"}
  },

})

wx.redirectTo(Object object)

關閉當前頁面,跳轉到應用內的某個頁面。但是不能跳轉到 tabbar 頁面。

參數

Object object


免責聲明!

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



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