2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.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/CONTRIBUTORS.txt
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/PATENTS.txt
11 @group Polymer Core Elements
13 The `core-ajax` element exposes `XMLHttpRequest` functionality.
17 url="http://gdata.youtube.com/feeds/api/videos/"
18 params='{"alt":"json", "q":"chrome"}'
20 on-core-response="{{handleResponse}}"></core-ajax>
22 With `auto` set to `true`, the element performs a request whenever
23 its `url` or `params` properties are changed.
25 Note: The `params` attribute must be double quoted JSON.
27 You can trigger a request explicitly by calling `go` on the
34 <link rel="import" href="core-xhr.html">
35 <polymer-element name="core-ajax" hidden attributes="url handleAs auto params response method headers body contentType withCredentials">
38 Polymer('core-ajax', {
40 * Fired when a response is received.
42 * @event core-response
46 * Fired when an error is received.
52 * Fired whenever a response or an error is received.
54 * @event core-complete
58 * The URL target of the request.
67 * Specifies what data to store in the `response` property, and
68 * to deliver as `event.response` in `response` events.
72 * `text`: uses `XHR.responseText`.
74 * `xml`: uses `XHR.responseXML`.
76 * `json`: uses `XHR.responseText` parsed as JSON.
78 * `arraybuffer`: uses `XHR.response`.
80 * `blob`: uses `XHR.response`.
82 * `document`: uses `XHR.response`.
91 * If true, automatically performs an Ajax request when either `url` or `params` changes.
100 * Parameters to send to the specified URL, as JSON.
103 * @type string (JSON)
109 * Returns the response object.
111 * @attribute response
118 * The HTTP method to use such as 'GET', 'POST', 'PUT', or 'DELETE'.
128 * HTTP request headers to send.
134 * url="http://somesite.com"
135 * headers='{"X-Requested-With": "XMLHttpRequest"}'
137 * on-core-response="{{handleResponse}}"></core-ajax>
146 * Optional raw body content to send when method === "POST".
150 * <core-ajax method="POST" auto url="http://somesite.com"
151 * body='{"foo":1, "bar":2}'>
161 * Content type to use when sending data.
163 * @attribute contentType
165 * @default 'application/x-www-form-urlencoded'
167 contentType: 'application/x-www-form-urlencoded',
170 * Set the withCredentials flag on the request.
172 * @attribute withCredentials
176 withCredentials: false,
179 * Additional properties to send to core-xhr.
181 * Can be set to an object containing default properties
182 * to send as arguments to the `core-xhr.request()` method
183 * which implements the low-level communication.
192 this.xhr = document.createElement('core-xhr');
195 receive: function(response, xhr) {
196 if (this.isSuccess(xhr)) {
197 this.processResponse(xhr);
204 isSuccess: function(xhr) {
205 var status = xhr.status || 0;
206 return !status || (status >= 200 && status < 300);
209 processResponse: function(xhr) {
210 var response = this.evalResponse(xhr);
211 this.response = response;
212 this.fire('core-response', {response: response, xhr: xhr});
215 error: function(xhr) {
216 var response = xhr.status + ': ' + xhr.responseText;
217 this.fire('core-error', {response: response, xhr: xhr});
220 complete: function(xhr) {
221 this.fire('core-complete', {response: xhr.status, xhr: xhr});
224 evalResponse: function(xhr) {
225 return this[(this.handleAs || 'text') + 'Handler'](xhr);
228 xmlHandler: function(xhr) {
229 return xhr.responseXML;
232 textHandler: function(xhr) {
233 return xhr.responseText;
236 jsonHandler: function(xhr) {
237 var r = xhr.responseText;
239 return JSON.parse(r);
241 console.warn('core-ajax caught an exception trying to parse reponse as JSON:');
242 console.warn('url:', this.url);
248 documentHandler: function(xhr) {
252 blobHandler: function(xhr) {
256 arraybufferHandler: function(xhr) {
260 urlChanged: function() {
261 if (!this.handleAs) {
262 var ext = String(this.url).split('.').pop();
265 this.handleAs = 'json';
272 paramsChanged: function() {
276 autoChanged: function() {
280 // TODO(sorvell): multiple side-effects could call autoGo
281 // during one micro-task, use a job to have only one action
285 this.goJob = this.job(this.goJob, this.go, 0);
290 * Performs an Ajax request to the specified URL.
295 var args = this.xhrArgs || {};
296 // TODO(sjmiles): we may want XHR to default to POST if body is set
297 args.body = this.body || args.body;
298 args.params = this.params || args.params;
299 if (args.params && typeof(args.params) == 'string') {
300 args.params = JSON.parse(args.params);
302 args.headers = this.headers || args.headers || {};
303 if (args.headers && typeof(args.headers) == 'string') {
304 args.headers = JSON.parse(args.headers);
306 if (this.contentType) {
307 args.headers['content-type'] = this.contentType;
309 if (this.handleAs === 'arraybuffer' || this.handleAs === 'blob' ||
310 this.handleAs === 'document') {
311 args.responseType = this.handleAs;
313 args.withCredentials = this.withCredentials;
314 args.callback = this.receive.bind(this);
316 args.method = this.method;
317 return args.url && this.xhr.request(args);