mui.ajax中文乱码

时间:2021-08-27 09:40:35

估计这是个bug:

 //mui 的 ajax 中文乱码
var url = 'http://api.juheapi.com/japi/toh?key=1f26c107d8864bdfb98202bc314ce0d5&month=11&day=25&v=2.0'
mui.ajax(url,{
dataType:'json',
type:'post',
timeout:5000,
success:function(data){
console.log(data);
var result = JSON.stringify(data.result);
console.log(result);
},
error:function(xhr,type,errorThrown){
console.log(type);
}
}); mui.plusReady(function(){
// 此种方式不会 中文乱码,
function a(){
var xhr = new plus.net.XMLHttpRequest();
xhr.onreadystatechange = function () {
switch ( xhr.readyState ) {
case 0:
alert( "xhr请求已初始化" );
break;
case 1:
alert( "xhr请求已打开" );
break;
case 2:
alert( "xhr请求已发送" );
break;
case 3:
alert( "xhr请求已响应");
break;
case 4:
if ( xhr.status == 200 ) {
alert( "xhr请求成功:"+xhr.responseText );
} else {
alert( "xhr请求失败:"+xhr.readyState );
}
break;
default :
break;
}
}
xhr.open( "GET", url );
xhr.send();
}
// a();
}) //我们对其进行封装
function myAjax(url,postData,success,error){
// 此种方式不会 中文乱码,
var type = postData.type;
var timeout = postData.timeout;
var data = postData.data;
var xhr = new plus.net.XMLHttpRequest();
if(timeout&&timeout>0) xhr.timeout = timeout;
xhr.onreadystatechange = function () {
switch ( xhr.readyState ) {
case 0:
// alert( "xhr请求已初始化" );
break;
case 1:
// alert( "xhr请求已打开" );
break;
case 2:
// alert( "xhr请求已发送" );
break;
case 3:
// alert( "xhr请求已响应");
break;
case 4:
if ( xhr.status == 200 ) {
success(eval('('+xhr.responseText+')'));
} else {
error(xhr.readyState,xhr);
}
break;
default :
break;
}
}
if(data){
if(type=='post'||type=='get'){
xhr.open( type||"GET", url );
xhr.send(JSON.stringify(data));
}else{
throw new Error("type is undefined !")
}
}else{
if(type!='post'&&type!='get'){
throw new Error("type is undefined !")
}
xhr.open( type||"GET", url );
xhr.send();
} }
mui.myAjax = myAjax;
mui.plusReady(function(){
mui.myAjax(url,{
type:'post',
timeout:5000,
data:{}
},
function(data){
var result = data.result;
result = JSON.stringify(result);
console.log(result);
mui.alert(result);
},function(state,xhr){
console.log(state)
}
);
})

1.仅仅对ajax,简单的封装一下,如果你看不顺眼,就自己封装吧

2.涉及到mui的plus模块,故需真机调试