-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
139 lines (139 loc) · 3.65 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Created by denniszhang on 16/3/1.
*/
/*包含jquery
'use strict'
class Overlay {
constructor() {
$('body').append('<div id="overlay" class="Hide"></div>');
var that=this;
$('#overlay').on('click',function(){
that._close();
})
}
open(){
$('#overlay').removeClass('Hide');
}
_close(){
$('#overlay').addClass('Hide');
}
}
$(function() {
var overlay=new Overlay();
$('button').on('click', function () {
overlay.open();
})
})
*/
/*
* 去除jquery
* */
'use strict'
// 使用原生js 封装ajax
// 兼容xhr对象
function createXHR(){
if(typeof XMLHttpRequest != "undefined"){ // 非IE6浏览器
return new XMLHttpRequest();
}else if(typeof ActiveXObject != "undefined"){ // IE6浏览器
var version = [
"MSXML2.XMLHttp.6.0",
"MSXML2.XMLHttp.3.0",
"MSXML2.XMLHttp",
];
for(var i = 0; i < version.length; i++){
try{
return new ActiveXObject(version[i]);
}catch(e){
//跳过
}
}
}else{
throw new Error("您的系统或浏览器不支持XHR对象!");
}
}
// 转义字符
function params(data){
var arr = [];
for(var i in data){
arr.push(encodeURIComponent(i) + "=" + encodeURIComponent(data[i]));
}
return arr.join("&");
}
// 封装ajax
function ga_ajax(obj){
var xhr = createXHR();
obj.url = obj.url + "?rand=" + Math.random(); // 清除缓存
obj.data = params(obj.data); // 转义字符串
if(obj.method === "get"){ // 判断使用的是否是get方式发送
obj.url += obj.url.indexOf("?") == "-1" ? "?" + obj.data : "&" + obj.data;
}
// 异步
if(obj.async === true){
// 异步的时候需要触发onreadystatechange事件
xhr.onreadystatechange = function(){
// 执行完成
if(xhr.readyState == 4){
callBack();
}
}
}
xhr.open(obj.method,obj.url,obj.async); // false是同步 true是异步 // "demo.php?rand="+Math.random()+"&name=ga&ga",
if(obj.method === "post"){
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(obj.data);
}else{
xhr.send(null);
}
// xhr.abort(); // 取消异步请求
// 同步
if(obj.async === false){
callBack();
}
// 返回数据
function callBack(){
// 判断是否返回正确
if(xhr.status == 200){
obj.success(JSON.parse(xhr.responseText));
}else{
obj.Error("获取数据失败,错误代号为:"+xhr.status+"错误信息为:"+xhr.statusText);
}
}
}
class Overlay {
constructor() {
var node=document.createElement('div');
node.id='overlay';
node.className='Hide';
document.querySelector('body').appendChild(node);
var that=this;
node.addEventListener('click',function(){
that._close();
})
}
open(){
document.querySelector('#overlay').className='';
}
_close(){
document.querySelector('#overlay').className='Hide';
}
}
var overlay=new Overlay();
document.querySelector('button').addEventListener('click',function(){
overlay.open();
ga_ajax({
"method" : "get",
"url" : "./data.json",
"data" : {
"name": "gaga",
"age": 10000000,
"num": "12346&598"
},
success:function(res){
console.log(res.code)
},
Error : function(msg){
alert(msg);
},
"async" : true
})
})