Skip to content

Commit ad7cafc

Browse files
authored
feat(Ajax): support form post data (#5862)
* refactor: 支持表单提交功能 * chore: bump version 9.5.10
1 parent 358e500 commit ad7cafc

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.5.9</Version>
4+
<Version>9.5.10</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

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)