如何解决如何确定:net::ERR_NAME_NOT_RESOLVED
我有一个网站,其后端位于不同的主机上。我正在使用 XHRHttpRequest
提交表单数据。但是,有时我的提供商不可用,或者托管服务器上正在进行一些技术工作。并且在提交表单时,用户不会收到任何通知,因为服务器没有响应。控制台包含以下文本:net::ERR_NAME_NOT_RESOLVED
。所以问题是,如果再次发生这种情况,我该如何向用户显示错误?如何使用 JS 检测此错误?
const form = document.getElementById('contact_form');
form.addEventListener('submit',function(e) {
e.preventDefault();
const send = {
name: document.querySelector('input[name=name]').value,email: document.querySelector('input[name=email]').value,subject: document.querySelector('input[name=subject]').value,message: document.querySelector('input[name=message]').value
};
const jsonString = JSON.stringify(send);
const xhr = new XMLHttpRequest();
const action = form.getAttribute('action');
xhr.open('POST',action,true);
xhr.setRequestHeader('Content-type','text/plain');
xhr.onload = function() {
if (this.readyState == 4 && this.status == 200) {
alert(xhr.responseText);
};
xhr.send(jsonString);
});
我尝试这样做:
if (this.readyState == 0) {
alert('The server is temporarily unavailable,please try again later!');
}
和
if (this.status == 503) {
alert('The server is temporarily unavailable,please try again later!');
}
但是还是不行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。