0
|
1 //>>excludeStart("exclude", pragmas.exclude);
|
|
2 define([ "shoestring" ], function(){
|
|
3 //>>excludeEnd("exclude");
|
|
4
|
|
5 /**
|
|
6 * Make an HTTP request to a url.
|
|
7 *
|
|
8 * **NOTE** the following options are supported:
|
|
9 *
|
|
10 * - *method* - The HTTP method used with the request. Default: `GET`.
|
|
11 * - *data* - Raw object with keys and values to pass with request as query params. Default `null`.
|
|
12 * - *headers* - Set of request headers to add. Default `{}`.
|
|
13 * - *async* - Whether the opened request is asynchronouse. Default `true`.
|
|
14 * - *success* - Callback for successful request and response. Passed the response data.
|
|
15 * - *error* - Callback for failed request and response.
|
|
16 * - *cancel* - Callback for cancelled request and response.
|
|
17 *
|
|
18 * @param {string} url The url to request.
|
|
19 * @param {object} options The options object, see Notes.
|
|
20 * @return shoestring
|
|
21 * @this shoestring
|
|
22 */
|
|
23
|
|
24 shoestring.ajax = function( url, options ) {
|
|
25 var params = "", req = new XMLHttpRequest(), settings, key;
|
|
26
|
|
27 settings = shoestring.extend( {}, shoestring.ajax.settings );
|
|
28
|
|
29 if( options ){
|
|
30 shoestring.extend( settings, options );
|
|
31 }
|
|
32
|
|
33 if( !url ){
|
|
34 url = settings.url;
|
|
35 }
|
|
36
|
|
37 if( !req || !url ){
|
|
38 return;
|
|
39 }
|
|
40
|
|
41 // create parameter string from data object
|
|
42 if( settings.data ){
|
|
43 for( key in settings.data ){
|
|
44 if( settings.data.hasOwnProperty( key ) ){
|
|
45 if( params !== "" ){
|
|
46 params += "&";
|
|
47 }
|
|
48 params += encodeURIComponent( key ) + "=" +
|
|
49 encodeURIComponent( settings.data[key] );
|
|
50 }
|
|
51 }
|
|
52 }
|
|
53
|
|
54 // append params to url for GET requests
|
|
55 if( settings.method === "GET" && params ){
|
|
56 //>>includeStart("development", pragmas.development);
|
|
57 if( url.indexOf("?") >= 0 ){
|
|
58 shoestring.error( 'ajax-url-query' );
|
|
59 }
|
|
60 //>>includeEnd("development");
|
|
61
|
|
62 url += "?" + params;
|
|
63 }
|
|
64
|
|
65 req.open( settings.method, url, settings.async );
|
|
66
|
|
67 if( req.setRequestHeader ){
|
|
68 req.setRequestHeader( "X-Requested-With", "XMLHttpRequest" );
|
|
69
|
|
70 // Set 'Content-type' header for POST requests
|
|
71 if( settings.method === "POST" && params ){
|
|
72 req.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
|
|
73 }
|
|
74
|
|
75 for( key in settings.headers ){
|
|
76 if( settings.headers.hasOwnProperty( key ) ){
|
|
77 req.setRequestHeader(key, settings.headers[ key ]);
|
|
78 }
|
|
79 }
|
|
80 }
|
|
81
|
|
82 req.onreadystatechange = function () {
|
|
83 if( req.readyState === 4 ){
|
|
84 // Trim the whitespace so shoestring('<div>') works
|
|
85 var res = (req.responseText || '').replace(/^\s+|\s+$/g, '');
|
|
86 if( req.status.toString().indexOf( "0" ) === 0 ){
|
|
87 return settings.cancel( res, req.status, req );
|
|
88 }
|
|
89 else if ( req.status.toString().match( /^(4|5)/ ) && RegExp.$1 ){
|
|
90 return settings.error( res, req.status, req );
|
|
91 }
|
|
92 else if (settings.success) {
|
|
93 return settings.success( res, req.status, req );
|
|
94 }
|
|
95 }
|
|
96 };
|
|
97
|
|
98 if( req.readyState === 4 ){
|
|
99 return req;
|
|
100 }
|
|
101
|
|
102 // Send request
|
|
103 if( settings.method === "POST" && params ){
|
|
104 req.send( params );
|
|
105 } else {
|
|
106 req.send();
|
|
107 }
|
|
108
|
|
109 return req;
|
|
110 };
|
|
111
|
|
112 shoestring.ajax.settings = {
|
|
113 success: function(){},
|
|
114 error: function(){},
|
|
115 cancel: function(){},
|
|
116 method: "GET",
|
|
117 async: true,
|
|
118 data: null,
|
|
119 headers: {}
|
|
120 };
|
|
121
|
|
122 //>>excludeStart("exclude", pragmas.exclude);
|
|
123 });
|
|
124 //>>excludeEnd("exclude");
|