鍍金池/ 問答/Python  HTML/ vue.js中,已經(jīng)離開頁面前往新的路由了,上一個(gè)頁面的ajax調(diào)用返回的錯(cuò)誤還

vue.js中,已經(jīng)離開頁面前往新的路由了,上一個(gè)頁面的ajax調(diào)用返回的錯(cuò)誤還是能顯示

我在一個(gè)頁面中執(zhí)行api ajax調(diào)用,catch異常時(shí),用element ui提供的$message函數(shù)打印錯(cuò)誤提示,當(dāng)ajax調(diào)用正在進(jìn)行中時(shí),點(diǎn)擊側(cè)邊欄導(dǎo)航菜單切換到其它頁面(router-link變了),然后在新的頁面上彈出了ajax調(diào)用超時(shí)的錯(cuò)誤提示,既然已經(jīng)離開了,能不能讓它不要顯示了?

      apiListProjects(platform_id, 0)
        .then(response => {
          if (response.data.platform_id === this.platformId.toString()) {
            this.projectList = response.data.projects
            this.currentPlatformLoadingFromParent.bol = false
          }
        })
        .catch((err) => {
          this.currentPlatformLoadingFromParent.bol = false
          if (err.response) {
            this.$message({
              type: 'error',
              message: '獲取項(xiàng)目列表失敗,錯(cuò)誤碼:' + err.response.status,
            })
          } else {
            this.$message({
              type: 'error',
              message: '獲取項(xiàng)目列表失敗,后端沒有響應(yīng)!',
            })
          }
        })
回答
編輯回答
故林

1.可以終止ajax,當(dāng)你點(diǎn)擊側(cè)邊欄導(dǎo)航菜單時(shí),如果用的原生ajax請(qǐng)求方法利用XMLHttpRequest對(duì)象有abort()方法;
2.如果是用的jquery,如何終止JQUERY的$.AJAX請(qǐng)求

var request = $.get(“ajax.aspx”,{id:1},function(data){
  //do something
});
//終止請(qǐng)求動(dòng)作.
request.abort();

3.如果用的axios,可以利用CancelToken取消ajax請(qǐng)求,axios中取消請(qǐng)求

2017年7月11日 14:06
編輯回答
懶豬

用的是axios嗎

2017年2月26日 22:00