| OLD | NEW |
| 1 | 1 |
| 2 Polymer({ | 2 Polymer({ |
| 3 is: 'iron-request', | 3 is: 'iron-request', |
| 4 | 4 |
| 5 properties: { | 5 properties: { |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A reference to the XMLHttpRequest instance used to generate the | 8 * A reference to the XMLHttpRequest instance used to generate the |
| 9 * network request. | 9 * network request. |
| 10 * | 10 * |
| 11 * @attribute xhr | 11 * @attribute xhr |
| 12 * @type XMLHttpRequest | 12 * @type XMLHttpRequest |
| 13 * @default `new XMLHttpRequest` | 13 * @default `new XMLHttpRequest` |
| 14 */ | 14 */ |
| 15 xhr: { | 15 xhr: { |
| 16 type: Object, | 16 type: Object, |
| 17 notify: true, | 17 notify: true, |
| 18 readOnly: true, | 18 readOnly: true, |
| 19 value: function() { | 19 value: function() { |
| 20 return new XMLHttpRequest(); | 20 return new XMLHttpRequest(); |
| 21 } | 21 } |
| 22 }, | 22 }, |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * A reference to the parsed response body, if the `xhr` has completely | 25 * A reference to the parsed response body, if the `xhr` has completely |
| 26 * resolved. | 26 * resolved. |
| 27 * | 27 * |
| 28 * @attribute response | 28 * @attribute response |
| 29 * @type Object | 29 * @type {*} |
| 30 * @default null | 30 * @default null |
| 31 */ | 31 */ |
| 32 response: { | 32 response: { |
| 33 type: Object, | 33 type: Object, |
| 34 notify: true, | 34 notify: true, |
| 35 readOnly: true, | 35 readOnly: true, |
| 36 value: function() { | 36 value: function() { |
| 37 return null; | 37 return null; |
| 38 } | 38 } |
| 39 }, | 39 }, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 value: false, | 89 value: false, |
| 90 } | 90 } |
| 91 }, | 91 }, |
| 92 | 92 |
| 93 /** | 93 /** |
| 94 * Succeeded is true if the request succeeded. The request succeeded if the | 94 * Succeeded is true if the request succeeded. The request succeeded if the |
| 95 * status code is greater-than-or-equal-to 200, and less-than 300. Also, | 95 * status code is greater-than-or-equal-to 200, and less-than 300. Also, |
| 96 * the status code 0 is accepted as a success even though the outcome may | 96 * the status code 0 is accepted as a success even though the outcome may |
| 97 * be ambiguous. | 97 * be ambiguous. |
| 98 * | 98 * |
| 99 * @return boolean | 99 * @return {boolean} |
| 100 */ | 100 */ |
| 101 get succeeded() { | 101 get succeeded() { |
| 102 var status = this.xhr.status || 0; | 102 var status = this.xhr.status || 0; |
| 103 | 103 |
| 104 // Note: if we are using the file:// protocol, the status code will be 0 | 104 // Note: if we are using the file:// protocol, the status code will be 0 |
| 105 // for all outcomes (successful or otherwise). | 105 // for all outcomes (successful or otherwise). |
| 106 return status === 0 || | 106 return status === 0 || |
| 107 (status >= 200 && status < 300); | 107 (status >= 200 && status < 300); |
| 108 }, | 108 }, |
| 109 | 109 |
| 110 /** | 110 /** |
| 111 * Sends an HTTP request to the server and returns the XHR object. | 111 * Sends an HTTP request to the server and returns the XHR object. |
| 112 * | 112 * |
| 113 * @method request | |
| 114 * @param {{ | 113 * @param {{ |
| 115 * url: string, | 114 * url: string, |
| 116 * method: (string|undefined), | 115 * method: (string|undefined), |
| 117 * async: (boolean|undefined), | 116 * async: (boolean|undefined), |
| 118 * body: (ArrayBuffer|ArrayBufferView|Blob|Document|FormData|null|string|u
ndefined), | 117 * body: (ArrayBuffer|ArrayBufferView|Blob|Document|FormData|null|string|u
ndefined), |
| 119 * headers: (Object|undefined), | 118 * headers: (Object|undefined), |
| 120 * handleAs: (string|undefined), | 119 * handleAs: (string|undefined), |
| 121 * withCredentials: (boolean|undefined)}} options - | 120 * withCredentials: (boolean|undefined)}} options - |
| 122 * url The url to which the request is sent. | 121 * url The url to which the request is sent. |
| 123 * method The HTTP method to use, default is GET. | 122 * method The HTTP method to use, default is GET. |
| 124 * async By default, all requests are sent asynchronously. To send synch
ronous requests, | 123 * async By default, all requests are sent asynchronously. To send synch
ronous requests, |
| 125 * set to true. | 124 * set to true. |
| 126 * body The content for the request body for POST method. | 125 * body The content for the request body for POST method. |
| 127 * headers HTTP request headers. | 126 * headers HTTP request headers. |
| 128 * handleAs The response type. Default is 'text'. | 127 * handleAs The response type. Default is 'text'. |
| 129 * withCredentials Whether or not to send credentials on the request. De
fault is false. | 128 * withCredentials Whether or not to send credentials on the request. De
fault is false. |
| 130 * @return Promise | 129 * @return {Promise} |
| 131 */ | 130 */ |
| 132 send: function (options) { | 131 send: function (options) { |
| 133 var xhr = this.xhr; | 132 var xhr = this.xhr; |
| 134 | 133 |
| 135 if (xhr.readyState > 0) { | 134 if (xhr.readyState > 0) { |
| 136 return; | 135 return null; |
| 137 } | 136 } |
| 138 | 137 |
| 139 xhr.addEventListener('readystatechange', function () { | 138 xhr.addEventListener('readystatechange', function () { |
| 140 if (xhr.readyState === 4 && !this.aborted) { | 139 if (xhr.readyState === 4 && !this.aborted) { |
| 141 | 140 |
| 142 if (!this.succeeded) { | 141 if (!this.succeeded) { |
| 143 this.rejectCompletes(new Error('The request failed with status code:
' + this.xhr.status)); | 142 this.rejectCompletes(new Error('The request failed with status code:
' + this.xhr.status)); |
| 144 return; | 143 return; |
| 145 } | 144 } |
| 146 | 145 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 // In IE, `xhr.responseType` is an empty string when the response | 182 // In IE, `xhr.responseType` is an empty string when the response |
| 184 // returns. Hence, caching it as `xhr._responseType`. | 183 // returns. Hence, caching it as `xhr._responseType`. |
| 185 xhr.responseType = xhr._responseType = (options.handleAs || 'text'); | 184 xhr.responseType = xhr._responseType = (options.handleAs || 'text'); |
| 186 xhr.withCredentials = !!options.withCredentials; | 185 xhr.withCredentials = !!options.withCredentials; |
| 187 | 186 |
| 188 xhr.send(options.body); | 187 xhr.send(options.body); |
| 189 | 188 |
| 190 return this.completes; | 189 return this.completes; |
| 191 }, | 190 }, |
| 192 | 191 |
| 192 /** |
| 193 * Attempts to parse the response body of the XHR. If parsing succeeds, |
| 194 * the value returned will be deserialized based on the `responseType` |
| 195 * set on the XHR. |
| 196 * |
| 197 * @return {*} The parsed response, |
| 198 * or undefined if there was an empty response or parsing failed. |
| 199 */ |
| 193 parseResponse: function () { | 200 parseResponse: function () { |
| 194 var xhr = this.xhr; | 201 var xhr = this.xhr; |
| 195 var responseType = this.xhr.responseType || | 202 var responseType = this.xhr.responseType || |
| 196 this.xhr._responseType; | 203 this.xhr._responseType; |
| 197 // If we don't have a natural `xhr.responseType`, we prefer parsing | 204 // If we don't have a natural `xhr.responseType`, we prefer parsing |
| 198 // `xhr.responseText` over returning `xhr.response`.. | 205 // `xhr.responseText` over returning `xhr.response`.. |
| 199 var preferResponseText = !this.xhr.responseType; | 206 var preferResponseText = !this.xhr.responseType; |
| 200 | 207 |
| 201 try { | 208 try { |
| 202 switch (responseType) { | 209 switch (responseType) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 227 return xhr.response; | 234 return xhr.response; |
| 228 case 'text': | 235 case 'text': |
| 229 default: | 236 default: |
| 230 return xhr.responseText; | 237 return xhr.responseText; |
| 231 } | 238 } |
| 232 } catch (e) { | 239 } catch (e) { |
| 233 this.rejectCompletes(new Error('Could not parse response. ' + e.message)
); | 240 this.rejectCompletes(new Error('Could not parse response. ' + e.message)
); |
| 234 } | 241 } |
| 235 }, | 242 }, |
| 236 | 243 |
| 244 /** |
| 245 * Aborts the request. |
| 246 */ |
| 237 abort: function () { | 247 abort: function () { |
| 238 this._setAborted(true); | 248 this._setAborted(true); |
| 239 this.xhr.abort(); | 249 this.xhr.abort(); |
| 240 } | 250 } |
| 241 }); | 251 }); |
| OLD | NEW |