如果没有互联网连接,我想显示吐司,不知道该怎么做

I want to display a toast if internet connection is not there, got no idea how to do that

本文关键字:不知道 吐司 显示 互联网 连接 如果没有      更新时间:2023-09-26

在这里,我在提交表单时发送邮件。一切正常,唯一的问题是即使互联网连接不存在,用户也获得了成功。我希望如果互联网连接不存在,用户应该收到错误消息。

脚本

this.mail= function() {
var data = ({
    name :this.name,
    email :this.email
})
//Post Request
$http.post('/send', data).
     success(function(response, status, headers, config){
          $mdToast.show(
                $mdToast.simple()
                  .textContent('Your form has been submitted '+data.name)
                  .position($scope.getToastPosition())
                  .hideDelay(5000)
              );
    }). 
    error(function(response, status, headers, config) {
        $mdToast.show(
         $mdToast.simple()
           .textContent('Something went wrong, Please TRY AGAIN '+data.name)
           .position($scope.getToastPosition())
           .hideDelay(5000)
       );
});

服务器

function send(req, res) {
  console.log(req.body);
  var data= req.body
  smtpTransport.sendMail({ 
     from: "<email@gmail.com>", 
     to: data.email, 
     subject: "Website Submission from "+data.name,
     text: 'You have a new submission with the following details...,
  }, function(error, response){  //callback
     if(error){
         console.log(error);
     }if(error.code == "ENOENT"){
         console.log("no internet connection");
     }else{
         console.log(" Message sent "+data.name);
     }
     smtpTransport.close(); 
  });
  res.json(data);
}

https://docs.angularjs.org/api/ng/service/$http$http旧承诺方法成功和错误已被弃用。请改用标准的 then 方法。如果$httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将引发$http/遗留错误。

不要使用成功/错误。

你可以使用 JavaScript 代码

if(!window.navigator.onLine)
{
//you are not online
}