首页 > 代码库 > mui.ajax中文乱码

mui.ajax中文乱码

估计这是个bug:

 

  1 //mui 的 ajax 中文乱码  2             var url = ‘http://api.juheapi.com/japi/toh?key=1f26c107d8864bdfb98202bc314ce0d5&month=11&day=25&v=2.0‘  3             mui.ajax(url,{  4                 dataType:‘json‘,  5                 type:‘post‘,  6                 timeout:5000,  7                 success:function(data){  8                     console.log(data);  9                     var result = JSON.stringify(data.result); 10                     console.log(result); 11                 }, 12                 error:function(xhr,type,errorThrown){ 13                     console.log(type); 14                 } 15             }); 16              17             mui.plusReady(function(){ 18                 // 此种方式不会 中文乱码, 19                 function a(){ 20                     var xhr = new plus.net.XMLHttpRequest(); 21                     xhr.onreadystatechange = function () { 22                         switch ( xhr.readyState ) { 23                             case 0: 24                                 alert( "xhr请求已初始化" ); 25                             break; 26                             case 1: 27                                 alert( "xhr请求已打开" ); 28                             break; 29                             case 2: 30                                 alert( "xhr请求已发送" ); 31                             break; 32                             case 3: 33                                 alert( "xhr请求已响应"); 34                                 break; 35                             case 4: 36                                 if ( xhr.status == 200 ) { 37                                     alert( "xhr请求成功:"+xhr.responseText ); 38                                 } else { 39                                     alert( "xhr请求失败:"+xhr.readyState ); 40                                 } 41                                 break; 42                             default : 43                                 break; 44                         } 45                     } 46                     xhr.open( "GET", url ); 47                     xhr.send(); 48                 } 49 //                a(); 50             }) 51              52             //我们对其进行封装 53             function myAjax(url,postData,success,error){ 54                 // 此种方式不会 中文乱码, 55                 var type = postData.type; 56                 var timeout = postData.timeout; 57                 var data =http://www.mamicode.com/ postData.data; 58                 var xhr = new plus.net.XMLHttpRequest(); 59                 if(timeout&&timeout>0) xhr.timeout = timeout; 60                 xhr.onreadystatechange = function () { 61                     switch ( xhr.readyState ) { 62                         case 0: 63 //                            alert( "xhr请求已初始化" ); 64                         break; 65                         case 1: 66 //                            alert( "xhr请求已打开" ); 67                         break; 68                         case 2: 69 //                            alert( "xhr请求已发送" ); 70                         break; 71                         case 3: 72 //                            alert( "xhr请求已响应"); 73                             break; 74                         case 4: 75                             if ( xhr.status == 200 ) { 76                                 success(eval(‘(‘+xhr.responseText+‘)‘)); 77                             } else { 78                                 error(xhr.readyState,xhr); 79                             } 80                             break; 81                         default : 82                             break; 83                     } 84                 } 85                 if(data){ 86                     if(type==‘post‘||type==‘get‘){ 87                         xhr.open( type||"GET", url ); 88                         xhr.send(JSON.stringify(data)); 89                     }else{ 90                         throw new Error("type is undefined !") 91                     } 92                 }else{ 93                     if(type!=‘post‘&&type!=‘get‘){ 94                         throw new Error("type is undefined !") 95                     } 96                     xhr.open( type||"GET", url ); 97                     xhr.send(); 98                 } 99                 100             }101             mui.myAjax = myAjax;102             mui.plusReady(function(){103                 mui.myAjax(url,{104                             type:‘post‘,105                             timeout:5000,106                             data:{}107                         },108                     function(data){109                         var result = data.result;110                         result = JSON.stringify(result);111                         console.log(result);112                         mui.alert(result);113                     },function(state,xhr){114                         console.log(state)115                     }116                 );117             })

 

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

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

 

mui.ajax中文乱码