Skip to content

Commit 6f291d4

Browse files
committed
refactor: 支持表单提交功能
1 parent 358e500 commit 6f291d4

File tree

1 file changed

+12
-5
lines changed
  • src/BootstrapBlazor/wwwroot/modules

1 file changed

+12
-5
lines changed

src/BootstrapBlazor/wwwroot/modules/ajax.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
url: null,
55
data: null,
66
toJson: true,
7+
headers: { 'Content-Type': 'application/json; charset=utf-8' },
78
...option
89
}
910

@@ -14,13 +15,19 @@
1415

1516
let result = null;
1617
try {
17-
const { toJson, url, method, data } = option;
18+
const { toJson, url, method, data, headers } = option;
19+
if (headers['Content-Type'] === void 0) {
20+
headers['Content-Type'] = 'application/json; charset=utf-8'
21+
}
22+
const contentType = headers['Content-Type'];
23+
let postData = JSON.stringify(data);
24+
if (contentType.indexOf('application/x-www-form-urlencoded') > -1) {
25+
postData = new URLSearchParams(data).toString();
26+
}
1827
result = await fetch(url, {
1928
method: method,
20-
headers: {
21-
'Content-Type': 'application/json'
22-
},
23-
body: method === 'POST' ? JSON.stringify(data) : null
29+
headers: headers,
30+
body: postData
2431
});
2532
if (toJson === true) {
2633
result = await result.json()

0 commit comments

Comments
 (0)