5 * Fired when a response is received.
11 * Fired when an error is received.
17 * Fired whenever a response or an error is received.
19 * @event core-complete
23 * The URL target of the request.
32 * Specifies what data to store in the `response` property, and
33 * to deliver as `event.response` in `response` events.
37 * `text`: uses `XHR.responseText`.
39 * `xml`: uses `XHR.responseXML`.
41 * `json`: uses `XHR.responseText` parsed as JSON.
43 * `arraybuffer`: uses `XHR.response`.
45 * `blob`: uses `XHR.response`.
47 * `document`: uses `XHR.response`.
56 * If true, automatically performs an Ajax request when either `url` or `params` changes.
65 * Parameters to send to the specified URL, as JSON.
74 * Returns the response object.
83 * The HTTP method to use such as 'GET', 'POST', 'PUT', or 'DELETE'.
93 * HTTP request headers to send.
99 * url="http://somesite.com"
100 * headers='{"X-Requested-With": "XMLHttpRequest"}'
102 * on-core-response="{{handleResponse}}"></core-ajax>
111 * Optional raw body content to send when method === "POST".
115 * <core-ajax method="POST" auto url="http://somesite.com"
116 * body='{"foo":1, "bar":2}'>
126 * Content type to use when sending data.
128 * @attribute contentType
130 * @default 'application/x-www-form-urlencoded'
132 contentType
: 'application/x-www-form-urlencoded',
135 * Set the withCredentials flag on the request.
137 * @attribute withCredentials
141 withCredentials
: false,
144 * Additional properties to send to core-xhr.
146 * Can be set to an object containing default properties
147 * to send as arguments to the `core-xhr.request()` method
148 * which implements the low-level communication.
157 this.xhr
= document
.createElement('core-xhr');
160 receive: function(response
, xhr
) {
161 if (this.isSuccess(xhr
)) {
162 this.processResponse(xhr
);
169 isSuccess: function(xhr
) {
170 var status
= xhr
.status
|| 0;
171 return !status
|| (status
>= 200 && status
< 300);
174 processResponse: function(xhr
) {
175 var response
= this.evalResponse(xhr
);
176 this.response
= response
;
177 this.fire('core-response', {response
: response
, xhr
: xhr
});
180 error: function(xhr
) {
181 var response
= xhr
.status
+ ': ' + xhr
.responseText
;
182 this.fire('core-error', {response
: response
, xhr
: xhr
});
185 complete: function(xhr
) {
186 this.fire('core-complete', {response
: xhr
.status
, xhr
: xhr
});
189 evalResponse: function(xhr
) {
190 return this[(this.handleAs
|| 'text') + 'Handler'](xhr
);
193 xmlHandler: function(xhr
) {
194 return xhr
.responseXML
;
197 textHandler: function(xhr
) {
198 return xhr
.responseText
;
201 jsonHandler: function(xhr
) {
202 var r
= xhr
.responseText
;
204 return JSON
.parse(r
);
206 console
.warn('core-ajax caught an exception trying to parse reponse as JSON:');
207 console
.warn('url:', this.url
);
213 documentHandler: function(xhr
) {
217 blobHandler: function(xhr
) {
221 arraybufferHandler: function(xhr
) {
225 urlChanged: function() {
226 if (!this.handleAs
) {
227 var ext
= String(this.url
).split('.').pop();
230 this.handleAs
= 'json';
237 paramsChanged: function() {
241 autoChanged: function() {
245 // TODO(sorvell): multiple side-effects could call autoGo
246 // during one micro-task, use a job to have only one action
250 this.goJob
= this.job(this.goJob
, this.go
, 0);
255 * Performs an Ajax request to the specified URL.
260 var args
= this.xhrArgs
|| {};
261 // TODO(sjmiles): we may want XHR to default to POST if body is set
262 args
.body
= this.body
|| args
.body
;
263 args
.params
= this.params
|| args
.params
;
264 if (args
.params
&& typeof(args
.params
) == 'string') {
265 args
.params
= JSON
.parse(args
.params
);
267 args
.headers
= this.headers
|| args
.headers
|| {};
268 if (args
.headers
&& typeof(args
.headers
) == 'string') {
269 args
.headers
= JSON
.parse(args
.headers
);
271 if (this.contentType
) {
272 args
.headers
['content-type'] = this.contentType
;
274 if (this.handleAs
=== 'arraybuffer' || this.handleAs
=== 'blob' ||
275 this.handleAs
=== 'document') {
276 args
.responseType
= this.handleAs
;
278 args
.withCredentials
= this.withCredentials
;
279 args
.callback
= this.receive
.bind(this);
281 args
.method
= this.method
;
282 return args
.url
&& this.xhr
.request(args
);