uni-app的POST请求和GET请求

uni-app就像一个盗版的Vue, 和Vue很想,又不完全像

//微信小程序post请求
uni.request({  
    url: 'http://127.0.0.1:8000/api/test_post/', // Django后端API地址  
    method: 'POST',  //指定请求类型(POST还是GET)
    data: {  //数据都要放在这里,以键值对的方式
        key1: 'value1', 
        key2: 'value2'  
    },  
    success: (res) => {  
        if (res.data.massage =='success') {  
            uni.showToast({  
                title: '请求成功',  
                icon: 'success'  
            });  
            // 处理成功的响应数据  
            console.log(res.data);  
        } else {  
            uni.showToast({  
                title: '请求失败',  
                icon: 'none'  
            });  
            // 处理失败的响应数据  
            console.error(res.data);  
        }  
    },  
    fail: (err) => {  
        uni.showToast({  
            title: '请求出错',  
            icon: 'none'  
        });  
        console.error(err);  
    }  
});  

 

后端接收

from rest_framework.views import APIView
#from rest_framework.response import Response
#from rest_framework.permissions import IsAuthenticated
from django.http import JsonResponse
#user表

# from django.contrib.auth.models import User
# from icecream import ic

class TestPostView(APIView):
    
    #token验证
    #permission_classes = [IsAuthenticated]
    def post(self, request):
        #传输的数据都在request.data里面
        req_dtat=request.data
        

        print('req_dtat',req_dtat)
        return JsonResponse({'massage': 'success'})

接收结果

 

posted @ 2024-04-26 12:47  台友涛  阅读(27)  评论(0编辑  收藏  举报