Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(571)

Side by Side Diff: remoting/webapp/me2mom/xhr.js

Issue 8336004: Improve web-app type safety. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « remoting/webapp/me2mom/wcs_loader.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @fileoverview 6 * @fileoverview
7 * Simple utilities for making XHRs more pleasant. 7 * Simple utilities for making XHRs more pleasant.
8 */ 8 */
9 9
10 'use strict'; 10 'use strict';
11 11
12 /** @suppress {duplicate} */ 12 /** @suppress {duplicate} */
13 var remoting = remoting || {}; 13 var remoting = remoting || {};
14 14
15 /** Namespace for XHR functions */ 15 /** Namespace for XHR functions */
16 /** @type {Object} */
16 remoting.xhr = remoting.xhr || {}; 17 remoting.xhr = remoting.xhr || {};
17 18
18 (function() {
19
20 /** 19 /**
21 * Takes an associative array of parameters and urlencodes it. 20 * Takes an associative array of parameters and urlencodes it.
22 * 21 *
23 * @param {Object.<string>} paramHash The parameter key/value pairs. 22 * @param {Object.<string>} paramHash The parameter key/value pairs.
24 * @return {string} URLEncoded version of paramHash. 23 * @return {string} URLEncoded version of paramHash.
25 */ 24 */
26 remoting.xhr.urlencodeParamHash = function(paramHash) { 25 remoting.xhr.urlencodeParamHash = function(paramHash) {
27 var paramArray = []; 26 var paramArray = [];
28 for (var key in paramHash) { 27 for (var key in paramHash) {
29 paramArray.push(encodeURIComponent(key) + 28 paramArray.push(encodeURIComponent(key) +
30 '=' + encodeURIComponent(paramHash[key])); 29 '=' + encodeURIComponent(paramHash[key]));
31 } 30 }
32 if (paramArray.length > 0) { 31 if (paramArray.length > 0) {
33 return paramArray.join('&'); 32 return paramArray.join('&');
34 } 33 }
35 return ''; 34 return '';
36 } 35 };
37 36
38 /** 37 /**
39 * Execute an XHR GET asynchronously. 38 * Execute an XHR GET asynchronously.
40 * 39 *
41 * @param {string} url The base URL to GET, excluding parameters. 40 * @param {string} url The base URL to GET, excluding parameters.
42 * @param {function(XMLHttpRequest):void} onDone The function to call on 41 * @param {function(XMLHttpRequest):void} onDone The function to call on
43 * completion. 42 * completion.
44 * @param {(string|Object.<string>)} opt_parameters The request parameters, 43 * @param {(string|Object.<string>)=} opt_parameters The request parameters,
45 * either as an associative array, or a string. If it is a string, do 44 * either as an associative array, or a string. If it is a string, do
46 * not include the ? and be sure it is correctly URLEncoded. 45 * not include the ? and be sure it is correctly URLEncoded.
47 * @param {Object.<string>} opt_headers Additional headers to include on the 46 * @param {Object.<string>=} opt_headers Additional headers to include on the
48 * request. 47 * request.
49 * @param {boolean} opt_withCredentials Set the withCredentials flags in the 48 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the
50 * XHR. 49 * XHR.
51 * @return {XMLHttpRequest} The request object. 50 * @return {XMLHttpRequest} The request object.
52 */ 51 */
53 remoting.xhr.get = function(url, onDone, opt_parameters, opt_headers, 52 remoting.xhr.get = function(url, onDone, opt_parameters, opt_headers,
54 opt_withCredentials) { 53 opt_withCredentials) {
54 /** @type {XMLHttpRequest} */
55 var xhr = new XMLHttpRequest(); 55 var xhr = new XMLHttpRequest();
56 xhr.onreadystatechange = function() { 56 xhr.onreadystatechange = function() {
57 if (xhr.readyState != 4) { 57 if (xhr.readyState != 4) {
58 return; 58 return;
59 } 59 }
60 onDone(xhr); 60 onDone(xhr);
61 }; 61 };
62 62
63 // Add parameters into URL. 63 // Add parameters into URL.
64 if (typeof(opt_parameters) === 'string') { 64 if (typeof(opt_parameters) === 'string') {
(...skipping 23 matching lines...) Expand all
88 } else { 88 } else {
89 throw 'opt_headers must be associative array.'; 89 throw 'opt_headers must be associative array.';
90 } 90 }
91 91
92 if (opt_withCredentials) { 92 if (opt_withCredentials) {
93 xhr.withCredentials = true; 93 xhr.withCredentials = true;
94 } 94 }
95 95
96 xhr.send(null); 96 xhr.send(null);
97 return xhr; 97 return xhr;
98 } 98 };
99 99
100 /** 100 /**
101 * Execute an XHR POST asynchronously. 101 * Execute an XHR POST asynchronously.
102 * 102 *
103 * @param {string} url The base URL to POST, excluding parameters. 103 * @param {string} url The base URL to POST, excluding parameters.
104 * @param {function(XMLHttpRequest):void} onDone The function to call on 104 * @param {function(XMLHttpRequest):void} onDone The function to call on
105 * completion. 105 * completion.
106 * @param {(string|Object.<string>)} opt_parameters The request parameters, 106 * @param {(string|Object.<string>)=} opt_parameters The request parameters,
107 * either as an associative array, or a string. If it is a string, be 107 * either as an associative array, or a string. If it is a string, be
108 * sure it is correctly URLEncoded. 108 * sure it is correctly URLEncoded.
109 * @param {Object.<string>} opt_headers Additional headers to include on the 109 * @param {Object.<string>=} opt_headers Additional headers to include on the
110 * request. 110 * request.
111 * @param {boolean} opt_withCredentials Set the withCredentials flags in the 111 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the
112 * XHR. 112 * XHR.
113 * @return {void} Nothing. 113 * @return {void} Nothing.
114 */ 114 */
115 remoting.xhr.post = function(url, onDone, opt_parameters, opt_headers, 115 remoting.xhr.post = function(url, onDone, opt_parameters, opt_headers,
116 opt_withCredentials) { 116 opt_withCredentials) {
117 /** @type {XMLHttpRequest} */
117 var xhr = new XMLHttpRequest(); 118 var xhr = new XMLHttpRequest();
118 xhr.onreadystatechange = function() { 119 xhr.onreadystatechange = function() {
119 if (xhr.readyState != 4) { 120 if (xhr.readyState != 4) {
120 return; 121 return;
121 } 122 }
122 onDone(xhr); 123 onDone(xhr);
123 }; 124 };
124 125
125 // Add parameters into URL. 126 // Add parameters into URL.
126 var postData = ''; 127 var postData = '';
(...skipping 19 matching lines...) Expand all
146 // No problem here. Do nothing. 147 // No problem here. Do nothing.
147 } else { 148 } else {
148 throw 'opt_headers must be associative array.'; 149 throw 'opt_headers must be associative array.';
149 } 150 }
150 151
151 if (opt_withCredentials) { 152 if (opt_withCredentials) {
152 xhr.withCredentials = true; 153 xhr.withCredentials = true;
153 } 154 }
154 155
155 xhr.send(postData); 156 xhr.send(postData);
156 } 157 };
157
158 }());
OLDNEW
« no previous file with comments | « remoting/webapp/me2mom/wcs_loader.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698