前后端交互之Promise的用法
- 原生ajax对后台进行访问
- Promise的基本用法
- Promise处理ajax请求
- Promisr对象的then参数中的函数的返回值
- Promise常用的实例方法
- Promise常用的对象方法
01原生ajax对后台进行访问
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-1.12.4.js"></script>
</head>
<body>
<script>
$.ajax({
url: "http://127.0.0.1:5000/img_code?phone=15612121212",
success: function (data) {
console.log(data);
}
})
$.ajax({
url: "http://127.0.0.1:5000/news_list?id=3",
success: function (data) {
console.log(data);
}
})
$.ajax({
url: "http://127.0.0.1:5000/news_detail/2",
success: function (data) {
console.log(data);
}
})
</script>
</body>
</html>
02Promise的基本用法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var p = new Promise(function (resoive, reject) {
setTimeout(function () {
var flag = false
if (flag) {
resoive("hello")
} else {
reject("world")
}
}, 1000)
}).then(function (data) {
console.log(data);
}, function (data) {
console.log(data);
})
</script>
</body>
</html>
03Promise处理ajax请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-1.12.4.js"></script>
</head>
<body>
<script>
function queryData(url) {
var p = new Promise(function (resoive, reject) {
var xhr = new XMLHttpRequest()
xhr.open("get", url)
xhr.send()
xhr.onreadystatechange = function (data) {
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 300) {
resoive(xhr.responseText)
} else {
reject("请求失败")
}
}
}
})
return p
}
queryData("http://127.0.0.1:5000/news_detail/2").then(function (data) {
console.log(data);
return queryData("http://127.0.0.1:5000/img_code?phone=15612121212")
}).then(function (data) {
console.log(data);
return queryData("http://127.0.0.1:5000/news_list?id=3")
}).then(function (data) {
console.log(data);
})
</script>
</body>
</html>
04Promisr对象的then参数中的函数的返回值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function queryData(url) {
var p = new Promise(function (resoive, reject) {
var xhr = new XMLHttpRequest()
xhr.open("get", url)
xhr.send()
xhr.onreadystatechange = function (data) {
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 300) {
resoive(xhr.responseText)
} else {
reject("请求失败")
}
}
}
})
return p
}
queryData("http://127.0.0.1:5000/news_detail/2").then(function (data) {
console.log(data);
return queryData("http://127.0.0.1:5000/img_code?phone=15612121212")
}).then(function (data) {
console.log(data);
return data
}).then(function (data) {
console.log(data);
})
</script>
</body>
</html>
05Promise常用的实例方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function foo() {
return new Promise(function (resoive, reject) {
setTimeout(function () {
reject("请求失败")
}, 100)
})
}
foo()
.then(function (data) {
console.log(data);
}, function (data) {
console.log(data);
})
.catch(function (data) {
console.log(data);
})
.finally(function (data) {
console.log("finally");
})
</script>
</body>
</html>
06Promise常用的对象方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=\, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function queryData(url) {
var p = new Promise(function (resoive, reject) {
var xhr = new XMLHttpRequest()
xhr.open("get", url)
xhr.send()
xhr.onreadystatechange = function (data) {
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 300) {
resoive(xhr.responseText)
} else {
reject("请求失败")
}
}
}
})
return p
}
var p1 = queryData("http://127.0.0.1:5000/news_detail/2")
var p2 = queryData("http://127.0.0.1:5000/img_code?phone=15612121212")
var p3 = queryData("http://127.0.0.1:5000/news_list?id=3")
Promise.race([p1, p2, p3]).then((res) => {
console.log(res);
})
</script>
</body>
</html>