uniapp封装ajax请求

module.exports = {
config: function (name) {
var info = null;
if (name) {
var name2 = name.split("."); //字符分割
if (name2.length > 1) {
info = configdata[name2[0]][name2[1]] || null;
} else {
info = configdata[name] || null;
}
if (info == null) {
let web_config = cache.get(“web_config”);
if (web_config) {
if (name2.length > 1) {
info = web_config[name2[0]][name2[1]] || null;
} else {
info = web_config[name] || null;
}
}
}
}
return info;
},
post: function (url, data, header) {
header = header || “application/x-www-form-urlencoded”;
url = this.config(“APIHOST”) + url;
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: “POST”,
header: {
“content-type”: header
},
success: function (result) {
succ.call(self, result.data)
},
fail: function (e) {
error.call(self, e)
}
})
})
},
postT: function (url, data, header) {
header = header || “application/x-www-form-urlencoded”;
url = this.config(“APIHOST1”) + url;
let token = uni.getStorageSync(“token”);
if (token) {
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: “POST”,
header: {
“content-type”: header,
“token”: token
},
success: function (result) {
console.error(result);
succ.call(self, result.data)
},
fail: function (e) {
error.call(self, e)
}
})
})
} else {
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: “POST”,
header: {
“content-type”: header,
},
success: function (result) {
succ.call(self, result.data)
},
fail: function (e) {
error.call(self, e)
}
})
})
}
},

postJson: function (url, data, header) {
    header = header || "application/json";
    url = this.config("APIHOST1") + url;
    let token = uni.getStorageSync("token");
    if (token) {
        return new Promise((succ, error) => {
            uni.request({
                url: url,
                data: data,
                method: "POST",
                header: {
                    "content-type": header,
                    "token": token
                },
                success: function (result) {
                    console.error(result);
                    succ.call(self, result.data)
                },
                fail: function (e) {
                    error.call(self, e)
                }
            })
        })
    } else {
        return new Promise((succ, error) => {
            uni.request({
                url: url,
                data: data,
                method: "POST",
                header: {
                    "content-type": header,
                },
                success: function (result) {
                    succ.call(self, result.data)
                },
                fail: function (e) {
                    error.call(self, e)
                }
            })
        })
    }
},
getT: function (url, data, header) {
    header = header || "application/x-www-form-urlencoded";
    url = this.config("APIHOST1") + url;
    let token = uni.getStorageSync("token");
    if (token) {
        return new Promise((succ, error) => {
            uni.request({
                url: url,
                data: data,
                method: "GET",
                header: {
                    "content-type": header,
                    "token": token
                },
                success: function (result) {
                    succ.call(self, result.data)
                },
                fail: function (e) {
                    error.call(self, e)
                }
            })
        })
    } else {
        return new Promise((succ, error) => {
            uni.request({
                url: url,
                data: data,
                method: "GET",
                header: {
                    "content-type": header
                },
                success: function (result) {
                    succ.call(self, result.data)
                },
                fail: function (e) {
                    error.call(self, e)
                }
            })
        })
    }

},
posts: function (url, data, header) {
    header = header || "application/x-www-form-urlencoded";
    return new Promise((succ, error) => {
        uni.request({
            url: url,
            data: data,
            method: "POST",
            header: {
                "content-type": header
            },
            success: function (result) {
                succ.call(self, result.data)
            },
            fail: function (e) {
                error.call(self, e)
            }
        })
    })
},
postF: function (url, data, header) {
    header = header || "application/json";
    url = this.config("APIHOST") + url;
    return new Promise((succ, error) => {
        uni.request({
            url: url,
            data: data,
            method: "POST",
            header: {
                "content-type": header
            },
            success: function (result) {
                succ.call(self, result.data)
            },
            fail: function (e) {
                error.call(self, e)
            }
        })
    })
},
postFs: function (url, data, header) {
    header = header || "application/json";
    return new Promise((succ, error) => {
        uni.request({
            url: url,
            data: data,
            method: "POST",
            header: {
                "content-type": header
            },
            success: function (result) {
                succ.call(self, result.data)
            },
            fail: function (e) {
                error.call(self, e)
            }
        })
    })
},
get: function (url, data, header) {
    header = header || "application/x-www-form-urlencoded";
    url = this.config("APIHOST") + url;
    return new Promise((succ, error) => {
        uni.request({
            url: url,
            data: data,
            method: "GET",
            header: {
                "content-type": header
            },
            success: function (result) {
                succ.call(self, result.data)
            },
            fail: function (e) {
                error.call(self, e)
            }
        })
    })
},
getJd: function (url, data, header) {
    header = header || "application/x-www-form-urlencoded";
    return new Promise((succ, error) => {
        uni.request({
            url: url,
            data: data,
            method: "GET",
            header: {
                "content-type": header
            },
            success: function (result) {
                succ.call(self, result.data)
            },
            fail: function (e) {
                error.call(self, e)
            }
        })
    })
},
get1: function (url, data, header) {
    header = header || "application/json";
    url = this.config("APIHOST") + url;
    return new Promise((succ, error) => {
        uni.request({
            url: url,
            data: data,
            method: "GET",
            success: function (result) {
                succ.call(self, result.data)
            },
            fail: function (e) {
                error.call(self, e)
            }
        })
    })
}

}

uniapp封装axios请求可以通过以下步骤进行: 第一步:安装依赖 你可以使用axios-miniprogram-adapter这个依赖来适配小程序网络请求的适配器。这样可以避免uniapp在使用axios请求时报"adapter is not a function"错误。你可以通过以下命令安装依赖: ``` npm i axios@0.26.0 axios-miniprogram-adapter ``` 或者 ``` yarn add axios@0.26.0 axios-miniprogram-adapter ``` 第二步:axios二次封装uniapp中,你可以对axios进行二次封装,以便更好地适配uniapp请求需求。你可以参考以下代码示例: ```javascript // 引入axios和适配器 import axios from 'axios' import adapter from 'axios-miniprogram-adapter' // 创建axios实例 const instance = axios.create({ adapter }) // 设置基本URL instance.defaults.baseURL = 'your_base_url' // 封装请求函数 function request(options) { return new Promise((resolve, reject) => { instance(options) .then(response => { resolve(response.data) }) .catch(error => { reject(error) }) }) } // 导出请求函数 export default request ``` 这样,你就可以在uniapp中使用封装好的axios请求了。例如: ```javascript import request from 'your_request_file_path' // 发起请求 request({ url: '/api/example', method: 'GET', data: { // 请求参数 } }) .then(response => { // 处理响应数据 }) .catch(error => { // 处理错误 }) ``` 通过以上步骤,你可以在uniapp封装axios请求,并且使用适配器来解决小程序网络请求的问题。希望对你有帮助! #### 引用[.reference_title] - *1* *2* [uniapp 微信小程序axios库的封装及使用](https://blog.csdn.net/weixin_43743175/article/details/129704892)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [uniapp封装axios?大可不必那么麻烦。](https://blog.csdn.net/m0_56344602/article/details/125390024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值