Skip to content

Commit 071d9a6

Browse files
committed
refactor: 重构代码
1 parent eab5ccc commit 071d9a6

File tree

1 file changed

+16
-11
lines changed
  • src/BootstrapBlazor/wwwroot/modules

1 file changed

+16
-11
lines changed

src/BootstrapBlazor/wwwroot/modules/ajax.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,24 @@
1919
if (headers['Content-Type'] === void 0) {
2020
headers['Content-Type'] = 'application/json; charset=utf-8'
2121
}
22-
const contentType = headers['Content-Type'];
23-
let postData = null;
24-
if (data) {
25-
postData = JSON.stringify(data);
26-
if (contentType.indexOf('application/x-www-form-urlencoded') > -1) {
27-
postData = new URLSearchParams(data).toString();
22+
23+
const fetchOptions = {
24+
method: method,
25+
headers: headers
26+
}
27+
28+
if (method === 'PUT' || method === 'POST') {
29+
const contentType = headers['Content-Type'];
30+
let postData = null;
31+
if (data) {
32+
postData = JSON.stringify(data);
33+
if (contentType.indexOf('application/x-www-form-urlencoded') > -1) {
34+
postData = new URLSearchParams(data).toString();
35+
}
36+
fetchOptions.body = postData;
2837
}
2938
}
30-
result = await fetch(url, {
31-
method: method,
32-
headers: headers,
33-
body: postData
34-
});
39+
result = await fetch(url, fetchOptions);
3540
if (toJson === true) {
3641
result = await result.json()
3742
}

0 commit comments

Comments
 (0)