Skip to content

Commit d7c87c2

Browse files
committed
update
1 parent 93fb971 commit d7c87c2

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @Author: huangchengdu
3+
* @Date: 2017-01-16 16:21:53
4+
* @Last Modified by: huangchengdu
5+
* @Last Modified time: 2017-01-16 17:38:20
6+
*/
7+
8+
function getURL(url) {
9+
return new Promise(function (resolve,reject) {
10+
var req = new XMLHttpRequest();
11+
req.open('GET',url,true);
12+
req.onload = function() {
13+
if (req.status === 200) {
14+
resolve(req.responseText);
15+
}else{
16+
reject(new Error(req.statusText));
17+
}
18+
};
19+
req.onerror = function () {
20+
reject(new Error(req.statusText));
21+
};
22+
req.send();
23+
});
24+
}
25+
26+
var url = "http://httpbin.org/get";
27+
getURL(url).then(function onFulfilled(value) {
28+
console.log(value);
29+
}).catch(function onRejected(error) {
30+
console.log(error);
31+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<script type="text/javascript">
6+
function getURL(url) {
7+
return new Promise(function (resolve,reject) {
8+
var req = new XMLHttpRequest();
9+
req.open('GET',url,true);
10+
req.onload = function() {
11+
if (req.status === 200) {
12+
resolve(req.responseText);
13+
}else{
14+
reject(new Error(req.statusText));
15+
}
16+
};
17+
req.onerror = function () {
18+
reject(new Error(req.statusText));
19+
};
20+
req.send();
21+
});
22+
}
23+
24+
var url = "http://httpbin.org/get";
25+
getURL(url).then(function onFulfilled(value) {
26+
console.log(value);
27+
}).catch(function onRejected(error) {
28+
console.log(error);
29+
});
30+
31+
url = "http://httpbin.org/status/500";
32+
getURL(url).then(function onFulfilled(value) {
33+
console.log("success");
34+
}).catch(function onRejected(error) {
35+
console.error(error);
36+
});
37+
38+
var onFulfilled = function(value) {
39+
console.log(value);
40+
};
41+
var onRejected = function(error) {
42+
console.log(error);
43+
};
44+
getURL(url).then(onFulfilled,onRejected);
45+
46+
47+
48+
49+
</script>
50+
</head>
51+
<body>
52+
53+
</body>
54+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<style type="javascript/text">
6+
7+
</style>
8+
<style type="text/css"></style>
9+
</head>
10+
<body>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)