估計這是個bug:
[](javascript:void(0); "復制代碼")
~~~
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 = 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 })
~~~
[](javascript:void(0); "復制代碼")
1.僅僅對ajax,簡單的封裝一下,如果你看不順眼,就自己封裝吧
2.涉及到mui的plus模塊,故需真機調試
本人博客歡迎轉載!但請注明出處!本人博客若有侵犯他人之處,望見諒,請聯系我。希望互相關注,互相學習 --[PheonixHkbxoic](http://www.cnblogs.com/PheonixHkbxoic/)