Skip to content
This repository has been archived by the owner on Nov 19, 2018. It is now read-only.

Commit

Permalink
fix(headers): custom headers are not sent with submit request
Browse files Browse the repository at this point in the history
Regression caused by removal of core-ajax in 1.5.0 via #42.

fixes #49
  • Loading branch information
rnicholus committed Jan 29, 2015
1 parent 0408f5c commit 7489cb2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
13 changes: 12 additions & 1 deletion ajax-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,23 @@
},

sendRequest = function(options) {
var xhr = new XMLHttpRequest();
var xhr = new XMLHttpRequest(),
customHeaders = options.form.headers;

xhr.open(options.form.acceptableMethod, options.url || options.form.action);

xhr.withCredentials = !!options.form.cookies;

if (customHeaders) {
if (typeof(customHeaders) === 'string') {
customHeaders = JSON.parse(customHeaders);
}

Object.keys(customHeaders).forEach(function(headerName) {
xhr.setRequestHeader(headerName, customHeaders[headerName]);
});
}

options.contentType && xhr.setRequestHeader('Content-Type', options.contentType);

xhr.onreadystatechange = function() {
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajax-form",
"version": "1.5.0",
"version": "1.5.1",
"authors": [
"Ray Nicholus"
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"type": "git",
"url": "https://github.com/garstasio/ajax-form.git"
},
"version": "1.5.0"
"version": "1.5.1"
}
25 changes: 24 additions & 1 deletion test/ajax-form-tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<my-ce2></my-ce2>
</form>

<form is="ajax-form" id="typicalFormWithHeaders" method="post" headers='{"X-Cust-Header": "FooBar", "X-Requested-By": "XMLHttpRequest"}' action="test">
<input name="test1" type="text" value="foobar"/>
</form>

<form is="ajax-form" id="requiredFileInputForm" method="post" action="test">
<file-input required></file-input>
</form>
Expand Down Expand Up @@ -88,6 +92,7 @@

<script>
var typicalForm = document.querySelector('#typicalForm'),
typicalFormWithHeaders = document.querySelector('#typicalFormWithHeaders'),
requiredFileInputForm = document.querySelector('#requiredFileInputForm'),
jsonForm = document.querySelector('#jsonForm'),
formDataForm = document.querySelector('#formDataForm'),
Expand All @@ -101,6 +106,7 @@
it('Make sure Polymer and ajax-form are alive', function() {
expect(ajaxForm).to.exist;
expect(typicalForm).to.exist;
expect(typicalFormWithHeaders).to.exist;
expect(requiredFileInputForm).to.exist;
expect(jsonForm).to.exist;
expect(formDataForm).to.exist;
Expand Down Expand Up @@ -211,7 +217,7 @@
expect(fireCall.args[1][0].tagName).to.equal('FILE-INPUT');

expect(requiredFileInputForm.checkValidity()).to.equal(false);
})
});
});

describe('submit interception', function() {
Expand Down Expand Up @@ -259,6 +265,23 @@
});
});

describe('custom headers', function() {
it('sends all custom headers with the request', function() {
ajaxForm.domReady.call(typicalFormWithHeaders);
typicalFormWithHeaders.submit();

expect(requests[0].requestBody).to.have.string('test1=foobar');

var headers = requests[0].requestHeaders;
expect(headers['Content-Type']).to.have.string('application/x-www-form-urlencoded');
delete headers['Content-Type'];
expect(JSON.stringify(headers)).to.equal(JSON.stringify({
'X-Cust-Header': 'FooBar',
'X-Requested-By': 'XMLHttpRequest'
}));
});
});

describe('JSON encoding', function() {
it('JSON encodes the form fields and includes them in the payload', function() {
ajaxForm.domReady.call(jsonForm);
Expand Down

0 comments on commit 7489cb2

Please sign in to comment.