uni-app mui.ajax&&uni.request&&XMLHttpRequest跨域请求4g网络超时,wifi正常
uni-app mui.ajax&&uni.request&&XMLHttpRequest跨域请求4g网络超时,wifi正常
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| HBuilderX | 3.1.17 | - |
测试过的手机
安卓4-6 苹果7苹果sxmax
示例代码:
mui.ajax('http://admin.wsjnjs.cqgh.org/api/nominees/setlike',{
data:{
id: "6287", openid: undefined
},
crossDomain: true,
dataType:'json',//服务器返回json格式数据
type:'post',//HTTP请求类型
timeout: 10000,//超时时间设置为10秒;
headers:{'Content-Type':'application/json'},
success:function(data){
console.log(JSON.stringify(data))
//服务器返回响应,根据响应结果,分析是否登录成功;
},
error:function(xhr,type,errorThrown){
//异常处理;
console.log(type);
}
});
操作步骤:
运行这个函数请求即可
预期结果:
预期能和正常返回结果数
实际结果:
报超时错误
bug描述:
uni-app html5+
mui.ajax
uni.request
XMLHttpRequest
3种网络请求跨域模式
在4g网络下都超时
在wifi模式下正常
更多关于uni-app mui.ajax&&uni.request&&XMLHttpRequest跨域请求4g网络超时,wifi正常的实战教程也可以访问 https://www.itying.com/category-93-b0.html
换成 5+的 plus.net 接口发送请求吧,mui早已停止维护了
更多关于uni-app mui.ajax&&uni.request&&XMLHttpRequest跨域请求4g网络超时,wifi正常的实战教程也可以访问 https://www.itying.com/category-93-b0.html
plus.net.XMLHttpRequest和uni.request的跨域在4g环境下都用不了 我都已经测试过 var xhr = new plus.net.XMLHttpRequest(); // 创建跨域请求 // 请求返回 xhr.onload = function () { if (xhr.responseText.code == 1001) { voteQuantity = 30; } else { voteQuantity++; } } // 超时超时 xhr.ontimeout = function (e) { console.log(JSON.stringify(e), “-------”) } xhr.open( “POST”, “http://admin.wsjnjs.cqgh.org/api/nominees/setlike” ); xhr.send(JSON.stringify({id: “6287”, openid: undefined}));
plus.net.XMLHttpRequest和uni.request的跨域在4g环境下都用不了
我都已经测试过
var xhr = new plus.net.XMLHttpRequest(); // 创建跨域请求
// 请求返回
xhr.onload = function () {
if (xhr.responseText.code == 1001) {
voteQuantity = 30;
} else {
voteQuantity++;
}
}
// 超时超时
xhr.ontimeout = function (e) {
console.log(JSON.stringify(e), “-------”)
}
xhr.open( “POST”, “http://admin.wsjnjs.cqgh.org/api/nominees/setlike” );
xhr.send(JSON.stringify({id: “6287”, openid: undefined}));
刚看到你运行的是h5,h5跨域的问题是浏览器的限制,需要自己适配解决,可以参考文档 https://ask.dcloud.net.cn/article/35267
我是在手机上打包成app运行的 而且wifi环境跨域没问题 4g网络环境就不行 如果是配置问题那应该2种网络情况都是一样的吧
这是一个典型的网络环境差异导致的请求超时问题,主要可能原因如下:
-
DNS解析差异:4G网络和WiFi网络可能使用不同的DNS服务器,导致域名解析结果不同或解析超时
-
网络质量差异:4G网络可能存在更高的延迟或更不稳定的连接质量
-
运营商限制:某些运营商可能对特定端口或协议有限制
-
MTU设置差异:不同网络环境的MTU设置可能导致数据包分片问题
建议排查步骤:
- 检查DNS解析:
// 在4G网络下测试域名解析
plus.net.getCurrentType(function(type){
console.log('当前网络类型:' + type);
});
- 优化请求配置:
// 使用uni.request替代mui.ajax
uni.request({
url: 'http://admin.wsjnjs.cqgh.org/api/nominees/setlike',
method: 'POST',
data: {
id: "6287",
openid: undefined
},
timeout: 15000, // 适当延长超时时间
header: {
'Content-Type': 'application/json'
},
success: (res) => {
console.log(res.data);
},
fail: (err) => {
console.log('请求失败:', err);
}
});

