• uniapp对uni.request()的封装以及使用


    官方文档

    uni.request(OBJECT) | uni-app官网 (dcloud.net.cn)

    uni.request参数

    参数名说明
    url是写api地址的
    data是用来传值的
    对于 GET 方法,会将数据 转换为 query string。例如 { name: 'name', age: 18 } 转换后的结果是 name=name&age=18
    对于 POST 方法且 header['content-type']application/json 的数据,会进行 JSON 序列化。
    对于 POST 方法且 header['content-type']application/x-www-form-urlencoded 的数据,会将数据转换为 query string。
    header是写请求头信息的
    method必须大写,有效值在不同平台差异说明不同。
    GET
    POST
    PUT 不支持支付宝小程序
    DELETE 不支持支付宝小程序、头条小程序
    succes访问接口成功之后就会调用success参数为res
    data Object/String/ArrayBuffer 开发者服务器返回的数据
    statusCode Number 开发者服务器返回的 HTTP 状态码
    header Object 开发者服务器返回的 HTTP Response Header

    封装

    export const request = (url, params, method = "GET") => {
    	return new Promise((resolve, reject) => {
    		uni.request({
    			url: "https://demo.com/api/public/v1" + url,
    			method: method,
    			data: params, //传入组装的参数
    			header: {
    				'Content-Type': 'application/json'
    			},
    			success: function(result) {
    				if (result.statusCode !== 200) {
    					console.log("发送请求成功但是开发者服务器返回的 HTTP 状态码不是200", url, params, method, result);
    					const data = {
    						code: "502",
    						msg: "发送请求成功但是开发者服务器返回的 HTTP 状态码不是200",
    						data: result
    					}
    					resolve(data);
    				} else {
    					console.log("发送请求成功", url, params, method, result);
    					// 返回数据
    					resolve(result.data);
    				}
    			},
    			fail: function(error) {
    				// 返回数据
    				console.log("发送请求失败", url, params, method, error);
    				const data = {
    					code: "500",
    					msg: "发送请求失败",
    					data: {}
    				}
    				resolve(data)
    			}
    		})
    
    	})
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    使用

    定义

    import {
    	request
    } from "./request.js"
    
    export const indexDAO = {
    	getBanner() {
    		return request("/home/swiperdata2",{},"GGT")
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    引入

    <template>
    	<view class="page_content">
    
        </view>
    </template>
    
    <script>
    	import {
    		indexDAO
    	} from "netword/index.js"
    
    
    	export default {
    		components: {},
    		data() {
    			return {
    				banner: []
    			}
    		},
    		onLoad() {
    			this.loadBanner()
    		}
    		methods: {
    			async loadBanner() {
    				const banner = await indexDAO.getBanner()
    				if (banner.code == 200) {
    					this.banner = banner.data
    				}
    
    			}
    		}
    	}
    </script>
    
    
    <style lang="scss" scoped>
    
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    在这里插入图片描述

  • 相关阅读:
    经典循环神经网络(一)RNN及其在歌词数据集上的应用
    idea 集成 git 后使用的常用命令
    嵌入式单片机的高级编程技巧和优化
    JAVA 从入门到起飞 day8 面向对象01
    博客主题美化第二弹
    如何用 GPTs 帮你写科研项目申请书?
    Gnome-keyring如何进行密码的CRUD
    WebGL 渲染三维图形作为纹理贴到另一个三维物体表面
    Windows使用命令查看端口号占用情况并关闭进程
    使用 Huggingface Trainer 对自定义数据集进行文本分类
  • 原文地址:https://blog.csdn.net/weixin_44797182/article/details/138176727