uni-app 关于uni.request的问题

uni-app 关于uni.request的问题

示例代码:

uni.request({  
    url: '***',  
    method: 'POST',  
    header: {},  
    data: {},  
    success(res) {  
        console.log(res)  
    }  
})

操作步骤:

111

预期结果:

111

实际结果:

111

bug描述:

调用获取数据大于150kb以上的数据大概率会出现数据只有一半的情况,返回数据越大越容易出现,然后就会导致请求超时,请问是什么原因导致的呢?在manifest.json中h5下面的devServer配置了代理访问,是不是这个原因导致的呢?

信息类别 详细信息
产品分类 uniapp/H5
PC开发环境 Windows
PC开发环境版本 Windows 10 家庭中文版 版本号22H2
HBuilderX类型 正式
HBuilderX版本 4.29
浏览器平台 Chrome
浏览器版本 Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1
项目创建方式 HBuilderX


更多关于uni-app 关于uni.request的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app 关于uni.request的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在uni-app中使用uni.request进行网络请求是非常常见的操作。uni.request是一个封装好的HTTP请求接口,适用于发送网络请求到服务器,并获取服务器返回的数据。下面我将通过几个代码示例来展示如何使用uni.request进行不同类型的网络请求。

基本的GET请求

uni.request({
    url: 'https://example.com/api/data', // 请求的URL
    method: 'GET', // 请求方式
    success: (res) => {
        console.log('GET请求成功', res.data);
        // 处理返回的数据
    },
    fail: (err) => {
        console.error('GET请求失败', err);
        // 处理请求失败的情况
    }
});

带参数的GET请求

在GET请求中,参数通常附加在URL后面:

const params = {
    id: 123,
    name: 'example'
};

const queryString = Object.keys(params)
    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
    .join('&');

uni.request({
    url: `https://example.com/api/data?${queryString}`,
    method: 'GET',
    success: (res) => {
        console.log('带参数的GET请求成功', res.data);
    },
    fail: (err) => {
        console.error('带参数的GET请求失败', err);
    }
});

POST请求

uni.request({
    url: 'https://example.com/api/data',
    method: 'POST',
    data: {
        id: 123,
        name: 'example'
    },
    header: {
        'Content-Type': 'application/json' // 设置请求头,表示发送的是JSON数据
    },
    success: (res) => {
        console.log('POST请求成功', res.data);
    },
    fail: (err) => {
        console.error('POST请求失败', err);
    }
});

带有自定义header的请求

uni.request({
    url: 'https://example.com/api/data',
    method: 'GET',
    header: {
        'Authorization': 'Bearer YOUR_ACCESS_TOKEN', // 自定义的header,比如携带token
        'Custom-Header': 'customHeaderValue'
    },
    success: (res) => {
        console.log('带有自定义header的请求成功', res.data);
    },
    fail: (err) => {
        console.error('带有自定义header的请求失败', err);
    }
});

以上代码示例展示了如何在uni-app中使用uni.request进行基本的GET和POST请求,以及如何处理带参数的GET请求和自定义header的请求。根据实际需求,你可以调整URL、请求方法、请求数据以及请求头等参数。

回到顶部