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 * core-xhr can be used to perform XMLHttpRequests.
15 * <core-xhr id="xhr"></core-xhr>
17 * this.$.xhr.request({url: url, params: params, callback: callback});
23 <link rel=
"import" href=
"../polymer/polymer.html">
25 <polymer-element name=
"core-xhr" hidden
>
32 * Sends a HTTP request to the server and returns the XHR object.
35 * @param {Object} inOptions
36 * @param {String} inOptions.url The url to which the request is sent.
37 * @param {String} inOptions.method The HTTP method to use, default is GET.
38 * @param {boolean} inOptions.sync By default, all requests are sent asynchronously. To send synchronous requests, set to true.
39 * @param {Object} inOptions.params Data to be sent to the server.
40 * @param {Object} inOptions.body The content for the request body for POST method.
41 * @param {Object} inOptions.headers HTTP request headers.
42 * @param {String} inOptions.responseType The response type. Default is 'text'.
43 * @param {boolean} inOptions.withCredentials Whether or not to send credentials on the request. Default is false.
44 * @param {Object} inOptions.callback Called when request is completed.
45 * @returns {Object} XHR object.
47 request: function(options
) {
48 var xhr
= new XMLHttpRequest();
49 var url
= options
.url
;
50 var method
= options
.method
|| 'GET';
51 var async
= !options
.sync
;
53 var params
= this.toQueryString(options
.params
);
54 if (params
&& method
== 'GET') {
55 url
+= (url
.indexOf('?') > 0 ? '&' : '?') + params
;
57 var xhrParams
= this.isBodyMethod(method
) ? (options
.body
|| params
) : null;
59 xhr
.open(method
, url
, async
);
60 if (options
.responseType
) {
61 xhr
.responseType
= options
.responseType
;
63 if (options
.withCredentials
) {
64 xhr
.withCredentials
= true;
66 this.makeReadyStateHandler(xhr
, options
.callback
);
67 this.setRequestHeaders(xhr
, options
.headers
);
70 xhr
.onreadystatechange(xhr
);
75 toQueryString: function(params
) {
77 for (var n
in params
) {
79 n
= encodeURIComponent(n
);
80 r
.push(v
== null ? n
: (n
+ '=' + encodeURIComponent(v
)));
85 isBodyMethod: function(method
) {
86 return this.bodyMethods
[(method
|| '').toUpperCase()];
96 makeReadyStateHandler: function(xhr
, callback
) {
97 xhr
.onreadystatechange = function() {
98 if (xhr
.readyState
== 4) {
99 callback
&& callback
.call(null, xhr
.response
, xhr
);
104 setRequestHeaders: function(xhr
, headers
) {
106 for (var name
in headers
) {
107 xhr
.setRequestHeader(name
, headers
[name
]);