diff --git a/src/htmx.js b/src/htmx.js index 0da9cf43..33a96726 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -3111,6 +3111,11 @@ return (function () { var headers = getHeaders(elt, target, promptResponse); + + if (verb !== 'get' && !usesFormData(elt)) { + headers['Content-Type'] = 'application/x-www-form-urlencoded'; + } + if (etc.headers) { headers = mergeObjects(headers, etc.headers); } @@ -3124,10 +3129,6 @@ return (function () { var allParameters = mergeObjects(rawParameters, expressionVars); var filteredParameters = filterValues(allParameters, elt); - if (verb !== 'get' && !usesFormData(elt)) { - headers['Content-Type'] = 'application/x-www-form-urlencoded'; - } - if (htmx.config.getCacheBusterParam && verb === 'get') { filteredParameters['org.htmx.cache-buster'] = getRawAttribute(target, "id") || "true"; } diff --git a/test/core/api.js b/test/core/api.js index 53f5960a..431633d6 100644 --- a/test/core/api.js +++ b/test/core/api.js @@ -273,6 +273,44 @@ describe("Core htmx API test", function(){ div.innerHTML.should.equal("Clicked!"); }); + it('ajax api Content-Type header is application/x-www-form-urlencoded', function(){ + + this.server.respondWith("POST", "/test", function (xhr) { + var params = getParameters(xhr); + xhr.requestHeaders['Content-Type'].should.equal('application/x-www-form-urlencoded;charset=utf-8'); + params['i1'].should.equal("test"); + xhr.respond(200, {}, "Clicked!") + }); + var div = make("
"); + htmx.ajax("POST", "/test", {target:"#d1", values:{i1: 'test'}}) + this.server.respond(); + div.innerHTML.should.equal("Clicked!"); + }); + + it('ajax api Content-Type header override to application/json', function(){ + + this.server.respondWith("POST", "/test", function (xhr) { + var params = getParameters(xhr); + xhr.requestHeaders['Content-Type'].should.equal('application/json;charset=utf-8'); + params['i1'].should.equal("test"); + xhr.respond(200, {}, "Clicked!"); + }); + + var div = make("
"); + htmx.ajax('POST',"/test", { + target:'#d1', + swap:'innerHTML', + headers: { + 'Content-Type': 'application/json' + }, + values:{i1: 'test'} + }) + + this.server.respond(); + div.innerHTML.should.equal("Clicked!"); + }); + + it('can re-init with new attributes', function () { this.server.respondWith("PATCH", "/test", "patch"); this.server.respondWith("DELETE", "/test", "delete");