3***@qq.com
3***@qq.com
  • 发布:2025-06-02 14:06
  • 更新:2025-06-02 14:06
  • 阅读:33

为什么老是报出io.dcloud.uts.UTSJSONObject cannot be cast to uni.UNI88FAA73.ResponseImpl‌

分类:uni-app x

今天简单的封装了这行代码, 但不知道什么原因老是报错: io.dcloud.uts.UTSJSONObject cannot be cast to uni.UNI88FAA73.ResponseImpl‌;

代码如下,是不给使用 promise 吗?

// 封装的请求函数
function makeRequest<T, R>(method : string, url : string, data : T | null = null) : Promise<ResponseImpl<R>> {
return new Promise<ResponseImpl<R>>((resolve, reject) => {
uni.request<ResponseImpl<R> | string | null>({
// 判断当前 url 是否包含 http,如果包含则直接使用,否则拼接 BASE_URL
url: url.indexOf('http') != -1 ? url : BASE_URL + url,
method: method == 'QUERY' ? 'POST' : method,
header: {
'Content-Type': method === 'QUERY' ? 'application/x-www-form-urlencoded' : 'application/json',
'Authorization': ${appInfo.token_type}${appInfo.refreshToken}
},
data: data != null ? JSON.stringify(data) : '',
success: (res) => {
if (res.statusCode == 401) {
failedRequests.push({ method, url, data, resolve, reject });
if (refreshStatus == 1) {
if (appInfo.refreshToken.trim().length > 0) {
authorize()
} else {
failedRequests = []
uni.showToast({
title: "登陆过期请重新登陆!",
icon: 'none',
duration: 1000,
success() {
failedRequests = []
uni.navigateTo({
url: "/pages/login/login"
})
}
})
}
}
} else if (res.statusCode == 405) {
failedRequests = []
uni.showToast({
title: "登陆过期请重新登陆!",
icon: 'none',
duration: 2000,
success() {
failedRequests = []
uni.navigateTo({
url: "/pages/login/login"
})
}
})
reject(res.data);
} else if (res.statusCode == 200) {
if (res.data == null) {
resolve(new Response('请求失败'))
} else if (typeof res.data != "object") {
resolve(new Response(res.data.toString()))
} else {
resolve(res.data)
}
} else {
reject(res.data);
}
},
fail: (err) => {
console.log(err, '--------err---------');
reject(err);
}
});
});
}

2025-06-02 14:06 负责人:无 分享
已邀请:

要回复问题请先登录注册