File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ //基于promise封装ajax
2
+ // 返回一个新的Promise实例
3
+
4
+
5
+ // 创建HMLHttpRequest异步对象
6
+
7
+
8
+ // 调用open方法,打开url,与服务器建立链接(发送前的一些处理)
9
+
10
+ // 监听Ajax状态信息
11
+
12
+ // 如果xhr.readyState == 4(表示服务器响应完成,可以获取使用服务器的响应了)
13
+
14
+ // xhr.status == 200,返回resolve状态
15
+ // xhr.status == 404,返回reject状态
16
+
17
+ // xhr.readyState !== 4,把请求主体的信息基于send发送给服务器
18
+
19
+ function ajax ( url , method , data ) {
20
+ return new Promise ( ( resolve , reject ) => {
21
+ let xhr = new XMLHttpRequest
22
+ xhr . open ( url , method )
23
+ xhr . send ( data )
24
+ xhr . onreadystatechange = ( ) => {
25
+ if ( xhr . readyState && xhr . status == 200 ) {
26
+ resolve ( xhr . responseText )
27
+ } else {
28
+ reject ( xhr . status )
29
+ }
30
+ }
31
+ } )
32
+ }
Original file line number Diff line number Diff line change @@ -15,3 +15,6 @@ function newFlag(Fnc){
15
15
}
16
16
return obj
17
17
}
18
+
19
+ let a = newFlag ( Array )
20
+ console . log ( Array . isArray ( a ) ) ;
You can’t perform that action at this time.
0 commit comments