Skip to content

Commit f47ea84

Browse files
committed
feat(pat ajax): Add accept parameter with default text/html for ajax requests. We are mainly using text/html in Patternslib - if a JSON response is needed you need to configure it when using pat-ajax.
1 parent e64ecac commit f47ea84

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/pat/ajax/ajax.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ import registry from "../../core/registry";
1212
const log = logging.getLogger("pat.ajax");
1313

1414
export const parser = new Parser("ajax");
15+
parser.addArgument("accept", "text/html");
1516
parser.addArgument("url", function ($el) {
16-
return ($el.is("a")
17-
? $el.attr("href")
18-
: $el.is("form")
19-
? $el.attr("action")
20-
: ""
17+
return (
18+
$el.is("a") ? $el.attr("href") : $el.is("form") ? $el.attr("action") : ""
2119
).split("#")[0];
2220
});
2321

@@ -104,10 +102,15 @@ var _ = {
104102
args = {
105103
context: $el,
106104
data: [$el.serialize(), clickedData].filter(Boolean).join("&"),
105+
headers: {},
107106
url: cfg.url,
108107
method: $el.attr("method") ? $el.attr("method") : "GET",
109108
};
110109

110+
if (cfg.accept) {
111+
args.headers.Accept = cfg.accept;
112+
}
113+
111114
if (
112115
$el.is("form") &&
113116
$el.attr("method") &&

src/pat/ajax/ajax.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,23 @@ describe("pat-ajax", function () {
135135
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
136136
expect(ajaxargs.url).toEqual("else.html");
137137
});
138+
139+
// Accept
140+
it("Default accept header", function () {
141+
document.body.innerHTML = `<a class="pat-ajax" />`;
142+
registry.scan(document.body);
143+
jest.spyOn($, "ajax");
144+
document.body.querySelector(".pat-ajax").click();
145+
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
146+
expect(ajaxargs.headers).toEqual({ Accept: "text/html" });
147+
});
148+
it("Can set accept header", function () {
149+
document.body.innerHTML = `<a class="pat-ajax" data-pat-ajax="accept: */*"/>`;
150+
registry.scan(document.body);
151+
jest.spyOn($, "ajax");
152+
document.body.querySelector(".pat-ajax").click();
153+
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
154+
expect(ajaxargs.headers).toEqual({ Accept: "*/*" });
155+
});
138156
});
139157
});

0 commit comments

Comments
 (0)