ajax封装
<script>
class Fn {
static get(url, obj, fn) {
Fn.http("get", url, obj, fn);
}
static post(url, obj, fn) {
Fn.http("post", url, obj, fn);
}
static http(type, url, obj, fn) {
let str = "";
for (let key in obj) {
str += key + "=" + obj[key] + "&";
}
str = str.slice(0, str.length - 1);
let xhr = new XMLHttpRequest();
url = type == "get" ? url + "?" + str : url;
xhr.open(type, url);
type == "post" &&
xhr.setRequestHeader(
"content-type",
"application/x-www-form-urlencoded"
);
xhr.send(type == "post" && str);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
fn(xhr.response);
}
};
}
}
</script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。