如果没有互联网连接,我想显示一个敬酒,不知道该怎么做

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

脚本

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传统的承诺方法的成功和错误已被弃用。 改为使用标准然后方法。 如果$ httpProvider.useLegacyPromiseExtensions设置为false,那么这些方法将抛出$ http / legacy错误。

不要使用成功/错误。

您可以使用JavaScript代码

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