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 30 matching lines...) Expand all Loading... |
41 * @param {string} url The base URL to GET, excluding parameters. | 41 * @param {string} url The base URL to GET, excluding parameters. |
42 * @param {function(XMLHttpRequest):void} onDone The function to call on | 42 * @param {function(XMLHttpRequest):void} onDone The function to call on |
43 * completion. | 43 * completion. |
44 * @param {(string|Object.<string>)} opt_parameters The request parameters, | 44 * @param {(string|Object.<string>)} opt_parameters The request parameters, |
45 * either as an associative array, or a string. If it is a string, do | 45 * 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. | 46 * not include the ? and be sure it is correctly URLEncoded. |
47 * @param {Object.<string>} opt_headers Additional headers to include on the | 47 * @param {Object.<string>} opt_headers Additional headers to include on the |
48 * request. | 48 * request. |
49 * @param {boolean} opt_withCredentials Set the withCredentials flags in the | 49 * @param {boolean} opt_withCredentials Set the withCredentials flags in the |
50 * XHR. | 50 * XHR. |
51 * @return {void} Nothing. | 51 * @return {XMLHttpRequest} The request object. |
52 */ | 52 */ |
53 remoting.xhr.get = function(url, onDone, opt_parameters, opt_headers, | 53 remoting.xhr.get = function(url, onDone, opt_parameters, opt_headers, |
54 opt_withCredentials) { | 54 opt_withCredentials) { |
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 }; |
(...skipping 25 matching lines...) Expand all Loading... |
87 // No problem here. Do nothing. | 87 // No problem here. Do nothing. |
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 } | 98 } |
98 | 99 |
99 /** | 100 /** |
100 * Execute an XHR POST asynchronously. | 101 * Execute an XHR POST asynchronously. |
101 * | 102 * |
102 * @param {string} url The base URL to POST, excluding parameters. | 103 * @param {string} url The base URL to POST, excluding parameters. |
103 * @param {function(XMLHttpRequest):void} onDone The function to call on | 104 * @param {function(XMLHttpRequest):void} onDone The function to call on |
104 * completion. | 105 * completion. |
105 * @param {(string|Object.<string>)} opt_parameters The request parameters, | 106 * @param {(string|Object.<string>)} opt_parameters The request parameters, |
106 * 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 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 } | 149 } |
149 | 150 |
150 if (opt_withCredentials) { | 151 if (opt_withCredentials) { |
151 xhr.withCredentials = true; | 152 xhr.withCredentials = true; |
152 } | 153 } |
153 | 154 |
154 xhr.send(postData); | 155 xhr.send(postData); |
155 } | 156 } |
156 | 157 |
157 }()); | 158 }()); |
OLD | NEW |