| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | 2 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | 3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | 4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | 5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 6 Code distributed by Google as part of the polymer project is also | 6 Code distributed by Google as part of the polymer project is also |
| 7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | 7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 8 --> | 8 --> |
| 9 | 9 |
| 10 <link rel="import" href="../polymer/polymer.html"> | 10 <link rel="import" href="../polymer/polymer.html"> |
| 11 <link rel="import" href="../promise-polyfill/promise-polyfill-lite.html"> | 11 <link rel="import" href="../promise-polyfill/promise-polyfill-lite.html"> |
| 12 | 12 |
| 13 <!-- | 13 <!-- |
| 14 @group Iron Elements | |
| 15 | |
| 16 iron-request can be used to perform XMLHttpRequests. | 14 iron-request can be used to perform XMLHttpRequests. |
| 17 | 15 |
| 18 <iron-request id="xhr"></iron-request> | 16 <iron-request id="xhr"></iron-request> |
| 19 ... | 17 ... |
| 20 this.$.xhr.send({url: url, params: params}); | 18 this.$.xhr.send({url: url, params: params}); |
| 21 | |
| 22 @element iron-request | |
| 23 --> | 19 --> |
| 24 | |
| 25 <script> | 20 <script> |
| 26 Polymer({ | 21 Polymer({ |
| 27 is: 'iron-request', | 22 is: 'iron-request', |
| 28 | 23 |
| 29 properties: { | 24 properties: { |
| 30 | 25 |
| 31 /** | 26 /** |
| 32 * A reference to the XMLHttpRequest instance used to generate the | 27 * A reference to the XMLHttpRequest instance used to generate the |
| 33 * network request. | 28 * network request. |
| 34 * | 29 * |
| 35 * @attribute xhr | 30 * @attribute xhr |
| 36 * @type XMLHttpRequest | 31 * @type XMLHttpRequest |
| 37 * @default `new XMLHttpRequest` | 32 * @default `new XMLHttpRequest` |
| 38 */ | 33 */ |
| 39 xhr: { | 34 xhr: { |
| 40 type: Object, | 35 type: Object, |
| 41 notify: true, | 36 notify: true, |
| 42 readOnly: true, | 37 readOnly: true, |
| 43 value: function() { | 38 value: function() { |
| 44 return new XMLHttpRequest(); | 39 return new XMLHttpRequest(); |
| 45 } | 40 } |
| 46 }, | 41 }, |
| 47 | 42 |
| 48 /** | 43 /** |
| 49 * A reference to the parsed response body, if the `xhr` has completely | 44 * A reference to the parsed response body, if the `xhr` has completely |
| 50 * resolved. | 45 * resolved. |
| 51 * | 46 * |
| 52 * @attribute response | 47 * @attribute response |
| 53 * @type Object | 48 * @type {*} |
| 54 * @default null | 49 * @default null |
| 55 */ | 50 */ |
| 56 response: { | 51 response: { |
| 57 type: Object, | 52 type: Object, |
| 58 notify: true, | 53 notify: true, |
| 59 readOnly: true, | 54 readOnly: true, |
| 60 value: function() { | 55 value: function() { |
| 61 return null; | 56 return null; |
| 62 } | 57 } |
| 63 }, | 58 }, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 value: false, | 108 value: false, |
| 114 } | 109 } |
| 115 }, | 110 }, |
| 116 | 111 |
| 117 /** | 112 /** |
| 118 * Succeeded is true if the request succeeded. The request succeeded if the | 113 * Succeeded is true if the request succeeded. The request succeeded if the |
| 119 * status code is greater-than-or-equal-to 200, and less-than 300. Also, | 114 * status code is greater-than-or-equal-to 200, and less-than 300. Also, |
| 120 * the status code 0 is accepted as a success even though the outcome may | 115 * the status code 0 is accepted as a success even though the outcome may |
| 121 * be ambiguous. | 116 * be ambiguous. |
| 122 * | 117 * |
| 123 * @return boolean | 118 * @return {boolean} |
| 124 */ | 119 */ |
| 125 get succeeded() { | 120 get succeeded() { |
| 126 var status = this.xhr.status || 0; | 121 var status = this.xhr.status || 0; |
| 127 | 122 |
| 128 // Note: if we are using the file:// protocol, the status code will be 0 | 123 // Note: if we are using the file:// protocol, the status code will be 0 |
| 129 // for all outcomes (successful or otherwise). | 124 // for all outcomes (successful or otherwise). |
| 130 return status === 0 || | 125 return status === 0 || |
| 131 (status >= 200 && status < 300); | 126 (status >= 200 && status < 300); |
| 132 }, | 127 }, |
| 133 | 128 |
| 134 /** | 129 /** |
| 135 * Sends an HTTP request to the server and returns the XHR object. | 130 * Sends an HTTP request to the server and returns the XHR object. |
| 136 * | 131 * |
| 137 * @method request | |
| 138 * @param {{ | 132 * @param {{ |
| 139 * url: string, | 133 * url: string, |
| 140 * method: (string|undefined), | 134 * method: (string|undefined), |
| 141 * async: (boolean|undefined), | 135 * async: (boolean|undefined), |
| 142 * body: (ArrayBuffer|ArrayBufferView|Blob|Document|FormData|null|string|u
ndefined), | 136 * body: (ArrayBuffer|ArrayBufferView|Blob|Document|FormData|null|string|u
ndefined), |
| 143 * headers: (Object|undefined), | 137 * headers: (Object|undefined), |
| 144 * handleAs: (string|undefined), | 138 * handleAs: (string|undefined), |
| 145 * withCredentials: (boolean|undefined)}} options - | 139 * withCredentials: (boolean|undefined)}} options - |
| 146 * url The url to which the request is sent. | 140 * url The url to which the request is sent. |
| 147 * method The HTTP method to use, default is GET. | 141 * method The HTTP method to use, default is GET. |
| 148 * async By default, all requests are sent asynchronously. To send synch
ronous requests, | 142 * async By default, all requests are sent asynchronously. To send synch
ronous requests, |
| 149 * set to true. | 143 * set to true. |
| 150 * body The content for the request body for POST method. | 144 * body The content for the request body for POST method. |
| 151 * headers HTTP request headers. | 145 * headers HTTP request headers. |
| 152 * handleAs The response type. Default is 'text'. | 146 * handleAs The response type. Default is 'text'. |
| 153 * withCredentials Whether or not to send credentials on the request. De
fault is false. | 147 * withCredentials Whether or not to send credentials on the request. De
fault is false. |
| 154 * @return Promise | 148 * @return {Promise} |
| 155 */ | 149 */ |
| 156 send: function (options) { | 150 send: function (options) { |
| 157 var xhr = this.xhr; | 151 var xhr = this.xhr; |
| 158 | 152 |
| 159 if (xhr.readyState > 0) { | 153 if (xhr.readyState > 0) { |
| 160 return; | 154 return null; |
| 161 } | 155 } |
| 162 | 156 |
| 163 xhr.addEventListener('readystatechange', function () { | 157 xhr.addEventListener('readystatechange', function () { |
| 164 if (xhr.readyState === 4 && !this.aborted) { | 158 if (xhr.readyState === 4 && !this.aborted) { |
| 165 | 159 |
| 166 if (!this.succeeded) { | 160 if (!this.succeeded) { |
| 167 this.rejectCompletes(new Error('The request failed with status code:
' + this.xhr.status)); | 161 this.rejectCompletes(new Error('The request failed with status code:
' + this.xhr.status)); |
| 168 return; | 162 return; |
| 169 } | 163 } |
| 170 | 164 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 // In IE, `xhr.responseType` is an empty string when the response | 201 // In IE, `xhr.responseType` is an empty string when the response |
| 208 // returns. Hence, caching it as `xhr._responseType`. | 202 // returns. Hence, caching it as `xhr._responseType`. |
| 209 xhr.responseType = xhr._responseType = (options.handleAs || 'text'); | 203 xhr.responseType = xhr._responseType = (options.handleAs || 'text'); |
| 210 xhr.withCredentials = !!options.withCredentials; | 204 xhr.withCredentials = !!options.withCredentials; |
| 211 | 205 |
| 212 xhr.send(options.body); | 206 xhr.send(options.body); |
| 213 | 207 |
| 214 return this.completes; | 208 return this.completes; |
| 215 }, | 209 }, |
| 216 | 210 |
| 211 /** |
| 212 * Attempts to parse the response body of the XHR. If parsing succeeds, |
| 213 * the value returned will be deserialized based on the `responseType` |
| 214 * set on the XHR. |
| 215 * |
| 216 * @return {*} The parsed response, |
| 217 * or undefined if there was an empty response or parsing failed. |
| 218 */ |
| 217 parseResponse: function () { | 219 parseResponse: function () { |
| 218 var xhr = this.xhr; | 220 var xhr = this.xhr; |
| 219 var responseType = this.xhr.responseType || | 221 var responseType = this.xhr.responseType || |
| 220 this.xhr._responseType; | 222 this.xhr._responseType; |
| 221 // If we don't have a natural `xhr.responseType`, we prefer parsing | 223 // If we don't have a natural `xhr.responseType`, we prefer parsing |
| 222 // `xhr.responseText` over returning `xhr.response`.. | 224 // `xhr.responseText` over returning `xhr.response`.. |
| 223 var preferResponseText = !this.xhr.responseType; | 225 var preferResponseText = !this.xhr.responseType; |
| 224 | 226 |
| 225 try { | 227 try { |
| 226 switch (responseType) { | 228 switch (responseType) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 251 return xhr.response; | 253 return xhr.response; |
| 252 case 'text': | 254 case 'text': |
| 253 default: | 255 default: |
| 254 return xhr.responseText; | 256 return xhr.responseText; |
| 255 } | 257 } |
| 256 } catch (e) { | 258 } catch (e) { |
| 257 this.rejectCompletes(new Error('Could not parse response. ' + e.message)
); | 259 this.rejectCompletes(new Error('Could not parse response. ' + e.message)
); |
| 258 } | 260 } |
| 259 }, | 261 }, |
| 260 | 262 |
| 263 /** |
| 264 * Aborts the request. |
| 265 */ |
| 261 abort: function () { | 266 abort: function () { |
| 262 this._setAborted(true); | 267 this._setAborted(true); |
| 263 this.xhr.abort(); | 268 this.xhr.abort(); |
| 264 } | 269 } |
| 265 }); | 270 }); |
| 266 </script> | 271 </script> |
| 267 | 272 |
| OLD | NEW |