Skip to content

Commit

Permalink
refactor(init.html): 弃用 xhr 改用 fetch API
Browse files Browse the repository at this point in the history
  • Loading branch information
Lete114 committed Jul 19, 2022
1 parent 9c0210f commit ab1df35
Showing 1 changed file with 4 additions and 91 deletions.
95 changes: 4 additions & 91 deletions public/init.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,87 +150,6 @@
}
}
</style>
<script>
var ajax = (function () {
'use strict'
function t(e) {
return (
(t =
'function' == typeof Symbol && 'symbol' == typeof Symbol.iterator
? function (t) {
return typeof t
}
: function (t) {
return t && 'function' == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype
? 'symbol'
: typeof t
}),
t(e)
)
}
var e = 'GET',
n = encodeURIComponent
function o(t, o) {
switch (
((function (t) {
var o = t.data,
r = t.method === e
if (Object.keys(o).length && r) {
var a = []
for (var s in o) Object.prototype.hasOwnProperty.call(o, s) && a.push(n(s) + '=' + n(o[s]))
;(t.url += /\?$/.test(t.url) ? '' : '?'), (t.url += a.join('&'))
}
})(o),
t.open(o.method, o.url, o.async, o.username, o.password),
(function (t, e) {
for (var n in e.headers) t.setRequestHeader(n, e.headers[n])
})(t, o),
o.method)
) {
case e:
t.send()
break
case 'POST':
t.send(JSON.stringify(o.data))
}
}
return function (n) {
return n
? new Promise(function (r, a) {
'object' !== t(n) && (n = { url: n })
var s,
u = new XMLHttpRequest(),
c = Object.assign({ method: e, async: !0, data: {}, headers: {} }, n)
;(c.method = c.method.toUpperCase()),
o(u, c),
c.timeout &&
(s = setTimeout(function () {
return u.abort()
}, c.timeout)),
(u.onreadystatechange = function () {
try {
if (4 === u.readyState)
s && clearTimeout(s),
u.status >= 200 && u.status < 300
? r(
(function (t) {
try {
return JSON.parse(t)
} catch (e) {
return t
}
})(u.responseText)
)
: a(u)
} catch (t) {
a(t)
}
})
})
: n
}
})()
</script>

<div class="init">
<div class="title">Init Discuss</div>
Expand Down Expand Up @@ -279,16 +198,10 @@
}

submit.setAttribute('disabled', '')
data = await ajax({
url: location.href,
method: 'post',
data: {
type: 'INIT',
mail,
username,
password
}
})
const body = JSON.stringify({ type: 'INIT', mail, username, password })
const response = await fetch(location.href, { method: 'post', body })
data = await response.json()

msg.style.display = 'block'
msg.innerText = data.msg

Expand Down

0 comments on commit ab1df35

Please sign in to comment.