jq--ajax中止请求
生活随笔
收集整理的這篇文章主要介紹了
jq--ajax中止请求
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
比如我后端設(shè)置延遲3s再響應(yīng)給前端,我用的是node之koa2
router.get('/vueDemo/getStudents', async ( ctx ) => {//延遲3sasync function delay(time) {return new Promise(function(resolve, reject) {setTimeout(function(){resolve();}, time);});};await delay(3000);//向后斷返回?cái)?shù)據(jù)let st = await students.find();ctx.response.type = 'application/json';ctx.body = st;
}) ?
前端設(shè)置超時(shí)
<script>var url='http://localhost:3000/vueDemo/getStudents'//發(fā)起請(qǐng)求var getStudents=$.ajax({type: 'get',url:url,timeout:2000})getStudents.then(function (rep) {console.log(rep)},function () {//如果超過(guò)2s還未回應(yīng),中斷請(qǐng)求console.log('超過(guò)2s還未回應(yīng),中斷請(qǐng)求')getStudents.abort()}) </script>
?
?
前端設(shè)置非超時(shí)
<script>var url='http://localhost:3000/vueDemo/getStudents'//發(fā)起請(qǐng)求var getStudents=$.ajax({type: 'get',url:url,timeout:5000})getStudents.then(function (rep) {console.log(rep)},function () {//如果超過(guò)2s還未回應(yīng),中斷請(qǐng)求console.log('超過(guò)2s還未回應(yīng),中斷請(qǐng)求')getStudents.abort()}) </script>
?
轉(zhuǎn)載于:https://www.cnblogs.com/dshvv/p/7784415.html
總結(jié)
以上是生活随笔為你收集整理的jq--ajax中止请求的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [NOI 2015]荷马史诗
- 下一篇: 数据结构之算法