diff --git a/src/htmx.js b/src/htmx.js index 0e2e57a6d..68f317735 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -3905,9 +3905,9 @@ var htmx = (function() { }) } else { let resolvedTarget = resolveTarget(context.target) - // If target is supplied but can't resolve OR both target and source can't be resolved + // If target is supplied but can't resolve OR source is supplied but both target and source can't be resolved // then use DUMMY_ELT to abort the request with htmx:targetError to avoid it replacing body by mistake - if ((context.target && !resolvedTarget) || (!resolvedTarget && !resolveTarget(context.source))) { + if ((context.target && !resolvedTarget) || (context.source && !resolvedTarget && !resolveTarget(context.source))) { resolvedTarget = DUMMY_ELT } return issueAjaxRequest(verb, path, resolveTarget(context.source), context.event, diff --git a/test/core/api.js b/test/core/api.js index c131dfd05..290363911 100644 --- a/test/core/api.js +++ b/test/core/api.js @@ -278,6 +278,16 @@ describe('Core htmx API test', function() { div.innerHTML.should.equal('foo!') }) + it('ajax api falls back to targeting body if target and source not set', function() { + this.server.respondWith('GET', '/test', 'foo!') + var div = make("
") + const saveBody = document.body.innerHTML + htmx.ajax('GET', '/test', {}) + this.server.respond() + document.body.innerHTML.should.equal('foo!') + document.body.innerHTML = saveBody + }) + it('ajax api works with swapSpec', function() { this.server.respondWith('GET', '/test', "foo!
") var div = make("