Vue.js-如何确定axios请求是否没有响应数据

问题描述

我正在尝试测试用户提供的IP地址是否为预期的IP,因此我向该IP地址发送了一个预期正在运行API服务的请求,如果IP正确,则返回成功,但如果IP错误,它不返回任何响应数据,在Vue.js应用程序的前端,我希望能够知道发生了什么并向用户发出警报,如果成功的话我可以做到这一点但是如果没有任何反应,我该如何获得该错误消息?

这是我的代码

PublisherUrl().get('/api/publish-lv/').then(()=>{
           
            Toast.fire({
                icon: 'success',title: 'Publisher Connected Successfully'
            })
        }).catch((error)=>{
            console.log(error)
          
            Toast.fire({
                icon: 'error',title: 'Publisher Device Unreachable'
            })
           
        })
    }

解决方法

then中的回调函数具有一个参数响应,该响应返回数据,标头和状态...

PublisherUrl().get('/api/publish-lv/').then((response)=>{
  if(response.data.length===0){
     Toast.fire({
                icon: 'info',title: 'No data'
            })
  }else{
     Toast.fire({
                icon: 'success',title: 'Publisher Connected Successfully'
            })
  }

   }).catch((error)=>{
            console.log(error)
          
            Toast.fire({
                icon: 'error',title: 'Publisher Device Unreachable'
            })
           
        })
    }