-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
54 lines (47 loc) · 1.46 KB
/
request.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @namespace Request
*
* @requires `extend`
* @requires `QueryString`
* @requires `Class`
**/
var Request = {};
Request.XHR = new Class({
// default parameters
params : {
async : true,
url : '',
method : 'get',
data : '',
onSuccess : function ( response ) {}
},
// our XHR object
xhr : null,
initialize : function ( params ) {
extend( this.params, params );
// `url` is required
if ( ! this.params.url ) {
throw new Error('Please provide `url` parameter');
}
// create the XHR
this.xhr = new XMLHttpRequest();
// add event handler
this.xhr.onload = function ( e ) {
this.params.onSuccess( this.xhr.responseText );
}.bind( this );
},
send : function ( data ) {
// stringify data object
data = QueryString.stringify( extend( this.params.data, data || {} ) );
// in case the method is GET, embed the params into the url
if ( this.params.method.toLowerCase() == 'get' ) {
this.params.url += ( this.params.url.indexOf('?') > 0 ? '&' : '?' ) + data;
}
// open state
this.xhr.open( this.params.method.toUpperCase(), this.params.url, this.params.async );
// set proper header
this.xhr.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
// finally send the request
this.xhr.send( data );
}
});