|
38 | 38 |
|
39 | 39 | Polymer('core-xhr', {
|
40 | 40 |
|
41 |
| - makeReadyStateHandler: function(xhr, callback) { |
42 |
| - xhr.onreadystatechange = function() { |
43 |
| - if (xhr.readyState == 4) { |
44 |
| - callback && callback.call(null, xhr.response, xhr); |
45 |
| - } |
46 |
| - }; |
47 |
| - }, |
48 |
| - |
49 |
| - setRequestHeaders: function(xhr, headers) { |
50 |
| - if (headers) { |
51 |
| - for (var name in headers) { |
52 |
| - xhr.setRequestHeader(name, headers[name]); |
53 |
| - } |
54 |
| - } |
55 |
| - }, |
56 |
| - |
57 |
| - toQueryString: function(params) { |
58 |
| - var r = []; |
59 |
| - for (var n in params) { |
60 |
| - var v = params[n]; |
61 |
| - n = encodeURIComponent(n); |
62 |
| - r.push(v == null ? n : (n + '=' + encodeURIComponent(v))); |
63 |
| - } |
64 |
| - return r.join('&'); |
65 |
| - }, |
66 |
| - |
67 | 41 | /**
|
68 | 42 | * Sends a HTTP request to the server and returns the XHR object.
|
69 | 43 | *
|
|
85 | 59 | var url = options.url;
|
86 | 60 | var method = options.method || 'GET';
|
87 | 61 | var async = !options.sync;
|
| 62 | + // |
88 | 63 | var params = this.toQueryString(options.params);
|
89 | 64 | if (params && method == 'GET') {
|
90 | 65 | url += (url.indexOf('?') > 0 ? '&' : '?') + params;
|
91 | 66 | }
|
| 67 | + var xhrParams = this.isBodyMethod(method) ? (options.body || params) : null; |
| 68 | + // |
92 | 69 | xhr.open(method, url, async);
|
93 | 70 | if (options.responseType) {
|
94 | 71 | xhr.responseType = options.responseType;
|
|
98 | 75 | }
|
99 | 76 | this.makeReadyStateHandler(xhr, options.callback);
|
100 | 77 | this.setRequestHeaders(xhr, options.headers);
|
101 |
| - xhr.send(method == 'POST' ? (options.body || params) : null); |
| 78 | + xhr.send(xhrParams); |
102 | 79 | if (!async) {
|
103 | 80 | xhr.onreadystatechange(xhr);
|
104 | 81 | }
|
105 | 82 | return xhr;
|
| 83 | + }, |
| 84 | + |
| 85 | + toQueryString: function(params) { |
| 86 | + var r = []; |
| 87 | + for (var n in params) { |
| 88 | + var v = params[n]; |
| 89 | + n = encodeURIComponent(n); |
| 90 | + r.push(v == null ? n : (n + '=' + encodeURIComponent(v))); |
| 91 | + } |
| 92 | + return r.join('&'); |
| 93 | + }, |
| 94 | + |
| 95 | + isBodyMethod: function(method) { |
| 96 | + return this.bodyMethods[(method || '').toUpperCase()]; |
| 97 | + }, |
| 98 | + |
| 99 | + bodyMethods: { |
| 100 | + POST: 1, |
| 101 | + PUT: 1, |
| 102 | + DELETE: 1 |
| 103 | + }, |
| 104 | + |
| 105 | + makeReadyStateHandler: function(xhr, callback) { |
| 106 | + xhr.onreadystatechange = function() { |
| 107 | + if (xhr.readyState == 4) { |
| 108 | + callback && callback.call(null, xhr.response, xhr); |
| 109 | + } |
| 110 | + }; |
| 111 | + }, |
| 112 | + |
| 113 | + setRequestHeaders: function(xhr, headers) { |
| 114 | + if (headers) { |
| 115 | + for (var name in headers) { |
| 116 | + xhr.setRequestHeader(name, headers[name]); |
| 117 | + } |
| 118 | + } |
106 | 119 | }
|
107 | 120 |
|
108 | 121 | });
|
|
0 commit comments