Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions core-ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@homepage github.io
-->
<link rel="import" href="core-xhr.html">
<polymer-element name="core-ajax" hidden attributes="url handleAs auto params response error method headers body contentType withCredentials progress loading">
<polymer-element name="core-ajax" hidden attributes="url handleAs auto params response error method headers body bodyType contentType withCredentials progress loading">
<script>

Polymer('core-ajax', {
Expand Down Expand Up @@ -186,6 +186,23 @@
*/
body: null,

/**
* Optional raw body Type content to send when method === "POST".
*
* Possible value are 'text', 'json'
*
* Example:
*
* <core-ajax method="POST" auto url="http://somesite.com"
* body='{{jsonObj}}' bodyType='json'>
* </core-ajax>
*
* @attribute bodyType
* @type String
* @default text
*/
bodyType: 'text',

/**
* Content type to use when sending data.
*
Expand Down Expand Up @@ -347,6 +364,20 @@
}
},

getParsedBody: function () {
var parsedBody = this.body;
// stringify body
switch(this.bodyType) {
case 'text' :
// Do nothing, just send the value
break;
case 'json' :
parsedBody = JSON.stringify(parsedBody);
break;
}
return parsedBody;
},

/**
* Performs an Ajax request to the specified URL.
*
Expand All @@ -355,7 +386,7 @@
go: function() {
var args = this.xhrArgs || {};
// TODO(sjmiles): we may want XHR to default to POST if body is set
args.body = this.body || args.body;
args.body = this.getParsedBody() || args.body;
args.params = this.params || args.params;
if (args.params && typeof(args.params) == 'string') {
args.params = JSON.parse(args.params);
Expand Down