OLD | NEW |
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'; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 * 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 |
45 * not include the ? and be sure it is correctly URLEncoded. | 45 * not include the ? and be sure it is correctly URLEncoded. |
46 * @param {Object.<string>=} opt_headers Additional headers to include on the | 46 * @param {Object.<string>=} opt_headers Additional headers to include on the |
47 * request. | 47 * request. |
48 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the | 48 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the |
49 * XHR. | 49 * XHR. |
50 * @return {XMLHttpRequest} The request object. | 50 * @return {XMLHttpRequest} The request object. |
51 */ | 51 */ |
52 remoting.xhr.get = function(url, onDone, opt_parameters, opt_headers, | 52 remoting.xhr.get = function(url, onDone, opt_parameters, opt_headers, |
53 opt_withCredentials) { | 53 opt_withCredentials) { |
54 /** @type {XMLHttpRequest} */ | 54 return remoting.xhr.doMethod('GET', url, onDone, opt_parameters, |
55 var xhr = new XMLHttpRequest(); | 55 opt_headers, opt_withCredentials); |
56 xhr.onreadystatechange = function() { | |
57 if (xhr.readyState != 4) { | |
58 return; | |
59 } | |
60 onDone(xhr); | |
61 }; | |
62 | |
63 // Add parameters into URL. | |
64 if (typeof(opt_parameters) === 'string') { | |
65 if (opt_parameters.length > 0) { | |
66 url = url + '?' + opt_parameters; | |
67 } | |
68 } else if (typeof(opt_parameters) === 'object') { | |
69 var paramString = remoting.xhr.urlencodeParamHash(opt_parameters); | |
70 if (paramString.length > 0) { | |
71 url = url + '?' + paramString; | |
72 } | |
73 } else if (opt_parameters === undefined) { | |
74 // No problem here. Do nothing. | |
75 } else { | |
76 throw 'opt_parameters must be string or associated array.'; | |
77 } | |
78 | |
79 xhr.open('GET', url, true); | |
80 | |
81 // Add in request headers. | |
82 if (typeof(opt_headers) === 'object') { | |
83 for (var key in opt_headers) { | |
84 xhr.setRequestHeader(key, opt_headers[key]); | |
85 } | |
86 } else if (opt_headers === undefined) { | |
87 // No problem here. Do nothing. | |
88 } else { | |
89 throw 'opt_headers must be associative array.'; | |
90 } | |
91 | |
92 if (opt_withCredentials) { | |
93 xhr.withCredentials = true; | |
94 } | |
95 | |
96 xhr.send(null); | |
97 return xhr; | |
98 }; | 56 }; |
99 | 57 |
100 /** | 58 /** |
101 * Execute an XHR POST asynchronously. | 59 * Execute an XHR POST asynchronously. |
102 * | 60 * |
103 * @param {string} url The base URL to POST, excluding parameters. | 61 * @param {string} url The base URL to POST, excluding parameters. |
104 * @param {function(XMLHttpRequest):void} onDone The function to call on | 62 * @param {function(XMLHttpRequest):void} onDone The function to call on |
105 * completion. | 63 * completion. |
106 * @param {(string|Object.<string>)=} opt_parameters The request parameters, | 64 * @param {(string|Object.<string>)=} opt_parameters The request parameters, |
107 * either as an associative array, or a string. If it is a string, be | 65 * either as an associative array, or a string. If it is a string, be |
108 * sure it is correctly URLEncoded. | 66 * sure it is correctly URLEncoded. |
109 * @param {Object.<string>=} opt_headers Additional headers to include on the | 67 * @param {Object.<string>=} opt_headers Additional headers to include on the |
110 * request. | 68 * request. |
111 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the | 69 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the |
112 * XHR. | 70 * XHR. |
113 * @return {void} Nothing. | 71 * @return {XMLHttpRequest} The request object. |
114 */ | 72 */ |
115 remoting.xhr.post = function(url, onDone, opt_parameters, opt_headers, | 73 remoting.xhr.post = function(url, onDone, opt_parameters, opt_headers, |
116 opt_withCredentials) { | 74 opt_withCredentials) { |
| 75 return remoting.xhr.doMethod('POST', url, onDone, opt_parameters, |
| 76 opt_headers, opt_withCredentials); |
| 77 }; |
| 78 |
| 79 /** |
| 80 * Execute an XHR DELETE asynchronously. |
| 81 * |
| 82 * @param {string} url The base URL to DELETE, excluding parameters. |
| 83 * @param {function(XMLHttpRequest):void} onDone The function to call on |
| 84 * completion. |
| 85 * @param {(string|Object.<string>)=} opt_parameters The request parameters, |
| 86 * either as an associative array, or a string. If it is a string, be |
| 87 * sure it is correctly URLEncoded. |
| 88 * @param {Object.<string>=} opt_headers Additional headers to include on the |
| 89 * request. |
| 90 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the |
| 91 * XHR. |
| 92 * @return {XMLHttpRequest} The request object. |
| 93 */ |
| 94 remoting.xhr.remove = function(url, onDone, opt_parameters, opt_headers, |
| 95 opt_withCredentials) { |
| 96 return remoting.xhr.doMethod('DELETE', url, onDone, opt_parameters, |
| 97 opt_headers, opt_withCredentials); |
| 98 }; |
| 99 |
| 100 /** |
| 101 * Execute an XHR PUT asynchronously. |
| 102 * |
| 103 * @param {string} url The base URL to PUT, excluding parameters. |
| 104 * @param {function(XMLHttpRequest):void} onDone The function to call on |
| 105 * completion. |
| 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 |
| 108 * sure it is correctly URLEncoded. |
| 109 * @param {Object.<string>=} opt_headers Additional headers to include on the |
| 110 * request. |
| 111 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the |
| 112 * XHR. |
| 113 * @return {XMLHttpRequest} The request object. |
| 114 */ |
| 115 remoting.xhr.put = function(url, onDone, opt_parameters, opt_headers, |
| 116 opt_withCredentials) { |
| 117 return remoting.xhr.doMethod('PUT', url, onDone, opt_parameters, |
| 118 opt_headers, opt_withCredentials); |
| 119 }; |
| 120 |
| 121 /** |
| 122 * Execute an arbitrary HTTP method asynchronously. |
| 123 * |
| 124 * @param {string} methodName The HTTP method name, e.g. "GET", "POST" etc. |
| 125 * @param {string} url The base URL, excluding parameters. |
| 126 * @param {function(XMLHttpRequest):void} onDone The function to call on |
| 127 * completion. |
| 128 * @param {(string|Object.<string>)=} opt_parameters The request parameters, |
| 129 * either as an associative array, or a string. If it is a string, be |
| 130 * sure it is correctly URLEncoded. |
| 131 * @param {Object.<string>=} opt_headers Additional headers to include on the |
| 132 * request. |
| 133 * @param {boolean=} opt_withCredentials Set the withCredentials flags in the |
| 134 * XHR. |
| 135 * @return {XMLHttpRequest} The XMLHttpRequest object. |
| 136 */ |
| 137 remoting.xhr.doMethod = function(methodName, url, onDone, |
| 138 opt_parameters, opt_headers, |
| 139 opt_withCredentials) { |
117 /** @type {XMLHttpRequest} */ | 140 /** @type {XMLHttpRequest} */ |
118 var xhr = new XMLHttpRequest(); | 141 var xhr = new XMLHttpRequest(); |
119 xhr.onreadystatechange = function() { | 142 xhr.onreadystatechange = function() { |
120 if (xhr.readyState != 4) { | 143 if (xhr.readyState != 4) { |
121 return; | 144 return; |
122 } | 145 } |
123 onDone(xhr); | 146 onDone(xhr); |
124 }; | 147 }; |
125 | 148 |
126 // Add parameters into URL. | 149 var parameterString = ''; |
127 var postData = ''; | |
128 if (typeof(opt_parameters) === 'string') { | 150 if (typeof(opt_parameters) === 'string') { |
129 postData = opt_parameters; | 151 parameterString = opt_parameters; |
130 } else if (typeof(opt_parameters) === 'object') { | 152 } else if (typeof(opt_parameters) === 'object') { |
131 postData = remoting.xhr.urlencodeParamHash(opt_parameters); | 153 parameterString = remoting.xhr.urlencodeParamHash(opt_parameters); |
132 } else if (opt_parameters === undefined) { | 154 } else if (opt_parameters === undefined) { |
133 // No problem here. Do nothing. | 155 // No problem here. Do nothing. |
134 } else { | 156 } else { |
135 throw 'opt_parameters must be string or associated array.'; | 157 throw 'opt_parameters must be string or associated array.'; |
136 } | 158 } |
137 | 159 |
138 xhr.open('POST', url, true); | 160 var useBody = (methodName == 'POST') || (methodName == 'PUT'); |
139 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); | |
140 | 161 |
| 162 if (!useBody && parameterString != '') { |
| 163 url = url + '?' + parameterString; |
| 164 } |
| 165 |
| 166 xhr.open(methodName, url, true); |
| 167 if (methodName == 'POST') { |
| 168 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); |
| 169 } |
141 // Add in request headers. | 170 // Add in request headers. |
142 if (typeof(opt_headers) === 'object') { | 171 if (typeof(opt_headers) === 'object') { |
143 for (var key in opt_headers) { | 172 for (var key in opt_headers) { |
144 xhr.setRequestHeader(key, opt_headers[key]); | 173 xhr.setRequestHeader(key, opt_headers[key]); |
145 } | 174 } |
146 } else if (opt_headers === undefined) { | 175 } else if (opt_headers === undefined) { |
147 // No problem here. Do nothing. | 176 // No problem here. Do nothing. |
148 } else { | 177 } else { |
149 throw 'opt_headers must be associative array.'; | 178 throw 'opt_headers must be associative array.'; |
150 } | 179 } |
151 | 180 |
152 if (opt_withCredentials) { | 181 if (opt_withCredentials) { |
153 xhr.withCredentials = true; | 182 xhr.withCredentials = true; |
154 } | 183 } |
155 | 184 |
156 xhr.send(postData); | 185 xhr.send(useBody ? parameterString : null); |
| 186 return xhr; |
157 }; | 187 }; |
OLD | NEW |