class QLibClass {
requestTask = null as RequestTask | null
Post(url : string, params : UTSJSONObject | null) : Promise<UTSJSONObject> {
const reqHeader = {
"Content-Type": "application/json"
};
return new Promise((resolve, reject) => {
this.requestTask = uni.request<UTSJSONObject>({
url: url,
method:'POST',
data: params,
header: reqHeader,
timeout: 6000,
sslVerify: false,
withCredentials: false,
firstIpv4: false,
success: (res) => {
console.log(res)
resolve(res.data as UTSJSONObject)
},
fail(err) {
console.log(err)
//const err = '网络错误,请联系管理员'
reject(JSON.stringify(err))
}
});
})
}
}
export const QLib =new QLibClass();
- 发布:2023-12-05 16:46
- 更新:2023-12-05 16:46
- 阅读:600
产品分类: uni-app x/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: win10
HBuilderX版本号: 3.98
手机系统: Android
手机系统版本号: Android 14
手机厂商: 小米
手机机型: k60
示例代码:
操作步骤:
<script>
import { QLib } from "@/utils/QLib.uts"
export default {
data() {
return {
AccountName: '',
AccountPwd: "",
}
},
methods: {
DoLogin() {
const params : UTSJSONObject=JSON.parseObject('{"name":"Tom"}')!;
QLib.Post("https://baidu.com/666",params).then(r => {
console.log(r.getString('code'));
})
}
}
}
</script>
<script>
import { QLib } from "@/utils/QLib.uts"
export default {
data() {
return {
AccountName: '',
AccountPwd: "",
}
},
methods: {
DoLogin() {
const params : UTSJSONObject=JSON.parseObject('{"name":"Tom"}')!;
QLib.Post("https://baidu.com/666",params).then(r => {
console.log(r.getString('code'));
})
}
}
}
</script>
预期结果:
UniError(errSubject='uni-request', errCode=100002, errMsg='error message invalid json', data=null, cause='null',HttpStatus=404)
UniError(errSubject='uni-request', errCode=100002, errMsg='error message invalid json', data=null, cause='null',HttpStatus=404)
实际结果:
UniError(errSubject='uni-request', errCode=100002, errMsg='error message invalid json', data=null, cause='null')
UniError(errSubject='uni-request', errCode=100002, errMsg='error message invalid json', data=null, cause='null')
bug描述:
uni.request 请求接口返回404状态码 无法识别 返回错误 UniError(errSubject='uni-request', errCode=100002, errMsg='error message invalid json', data=null, cause='null')
0 个回复