1
1
/*
2
2
----------------------------------------------------------
3
- util/Request : 0.1.1 : 2014-10-17
3
+ util/Request : 0.1.1 : 2015-03-26
4
4
----------------------------------------------------------
5
5
util.request({
6
6
url: './dir/something.extension',
7
- body: 'test!',
8
- onerror: function(event) {
9
- console.log(event);
7
+ data: 'test!',
8
+ format: 'text', // text | xml | json | binary
9
+ responseType: 'text', // arraybuffer | blob | document | json | text
10
+ headers: {},
11
+ withCredentials: true, // true | false
12
+ ///
13
+ onerror: function(evt, percent) {
14
+ console.log(evt);
10
15
},
11
- onload : function(response ) {
12
- console.log(response. responseText);
16
+ onsuccess : function(evt, responseText ) {
17
+ console.log(responseText);
13
18
},
14
- onprogress: function(event ) {
15
- var percent = event.loaded / event.total * 100 >> 0 ;
19
+ onprogress: function(evt, percent ) {
20
+ percent = Math.round(percent * 100) ;
16
21
loader.create('thread', 'loading... ', percent);
17
22
}
18
23
});
@@ -24,41 +29,43 @@ if (typeof MIDI === 'undefined') MIDI = {};
24
29
25
30
var util = root . util || ( root . util = { } ) ;
26
31
27
- util . request = function ( params , onload , onerror , onprogress ) { 'use strict' ;
28
- if ( typeof ( params ) === 'string' ) params = { url : params } ;
32
+ util . request = function ( opts , onsuccess , onerror , onprogress ) { 'use strict' ;
33
+ if ( typeof opts === 'string' ) opts = { url : opts } ;
34
+ ///
35
+ var data = opts . data ;
36
+ var url = opts . url ;
37
+ var method = opts . method || ( opts . data ? 'POST' : 'GET' ) ;
38
+ var format = opts . format ;
39
+ var headers = opts . headers ;
40
+ var responseType = opts . responseType ;
41
+ var withCredentials = opts . withCredentials || false ;
29
42
///
30
- var body = params . body ;
31
- var url = params . url ;
32
- var headers = params . headers ;
33
- var responseType = params . responseType ;
34
- var withCredentials = params . withCredentials ;
35
- var asBinaryString = params . asBinaryString ;
36
- var onload = onload || params . onload ;
37
- var onerror = onerror || params . onerror ;
38
- var onprogress = onprogress || params . onprogress ;
43
+ var onsuccess = onsuccess || opts . onsuccess ;
44
+ var onerror = onerror || opts . onerror ;
45
+ var onprogress = onprogress || opts . onprogress ;
39
46
///
40
- if ( typeof ( NodeFS ) !== 'undefined' && root . loc . isLocalUrl ( url ) ) {
41
- NodeFS . readFile ( url , 'utf8' , function ( error , data ) {
42
- if ( error ) {
43
- onerror && onerror ( error , false ) ;
47
+ if ( typeof NodeFS !== 'undefined' && root . loc . isLocalUrl ( url ) ) {
48
+ NodeFS . readFile ( url , 'utf8' , function ( err , res ) {
49
+ if ( err ) {
50
+ onerror && onerror ( err ) ;
44
51
} else {
45
- onload && onload ( { responseText : data } ) ;
52
+ onsuccess && onsuccess ( { responseText : res } ) ;
46
53
}
47
54
} ) ;
48
55
return ;
49
56
}
50
57
///
51
58
var xhr = new XMLHttpRequest ( ) ;
52
- xhr . open ( body ? 'POST' : 'GET' , url , true ) ;
59
+ xhr . open ( method , url , true ) ;
53
60
///
54
61
if ( headers ) {
55
- for ( var key in headers ) {
56
- xhr . setRequestHeader ( key , headers [ key ] ) ;
62
+ for ( var type in headers ) {
63
+ xhr . setRequestHeader ( type , headers [ type ] ) ;
57
64
}
58
- } else if ( body ) { // set the default headers for POST
65
+ } else if ( data ) { // set the default headers for POST
59
66
xhr . setRequestHeader ( 'Content-type' , 'application/x-www-form-urlencoded' ) ;
60
67
}
61
- if ( asBinaryString ) { //- default to responseType="blob" when supported
68
+ if ( format === 'binary' ) { //- default to responseType="blob" when supported
62
69
if ( xhr . overrideMimeType ) {
63
70
xhr . overrideMimeType ( 'text/plain; charset=x-user-defined' ) ;
64
71
}
@@ -73,26 +80,59 @@ if (typeof MIDI === 'undefined') MIDI = {};
73
80
xhr . onerror = onerror ;
74
81
}
75
82
if ( onprogress && xhr . upload && 'onprogress' in xhr . upload ) {
76
- if ( body ) {
77
- xhr . upload . onprogress = onprogress ;
83
+ if ( data ) {
84
+ xhr . upload . onprogress = function ( evt ) {
85
+ onprogress . call ( xhr , evt , event . loaded / event . total ) ;
86
+ } ;
78
87
} else {
79
- xhr . onprogress = onprogress ;
88
+ xhr . addEventListener ( 'progress' , function ( evt ) {
89
+ var totalBytes = 0 ;
90
+ if ( evt . lengthComputable ) {
91
+ totalBytes = evt . total ;
92
+ } else if ( xhr . totalBytes ) {
93
+ totalBytes = xhr . totalBytes ;
94
+ } else {
95
+ var rawBytes = parseInt ( xhr . getResponseHeader ( 'Content-Length-Raw' ) ) ;
96
+ if ( isFinite ( rawBytes ) ) {
97
+ xhr . totalBytes = totalBytes = rawBytes ;
98
+ } else {
99
+ return ;
100
+ }
101
+ }
102
+ onprogress . call ( xhr , evt , evt . loaded / totalBytes ) ;
103
+ } ) ;
80
104
}
81
105
}
82
106
///
83
- xhr . onreadystatechange = function ( event ) {
107
+ xhr . onreadystatechange = function ( evt ) {
84
108
if ( xhr . readyState === 4 ) { // The request is complete
85
109
if ( xhr . status === 200 || // Response OK
86
110
xhr . status === 304 || // Not Modified
87
- xhr . status === 0 && root . client && root . client . cordova // Cordova quirk
111
+ xhr . status === 308 || // Permanent Redirect
112
+ xhr . status === 0 && root . client . cordova // Cordova quirk
88
113
) {
89
- onload && onload ( xhr ) ;
114
+ if ( onsuccess ) {
115
+ var res ;
116
+ if ( format === 'xml' ) {
117
+ res = evt . target . responseXML ;
118
+ } else if ( format === 'text' ) {
119
+ res = evt . target . responseText ;
120
+ } else if ( format === 'json' ) {
121
+ try {
122
+ res = JSON . parse ( evt . target . response ) ;
123
+ } catch ( err ) {
124
+ onerror && onerror . call ( xhr , evt ) ;
125
+ }
126
+ }
127
+ ///
128
+ onsuccess . call ( xhr , evt , res ) ;
129
+ }
90
130
} else {
91
- onerror && onerror ( event , false ) ;
131
+ onerror && onerror . call ( xhr , evt ) ;
92
132
}
93
133
}
94
134
} ;
95
- xhr . send ( body ) ;
135
+ xhr . send ( data ) ;
96
136
return xhr ;
97
137
} ;
98
138
0 commit comments