我想确保发送到我的服务器的网址是一个真实的网站( heroku app ),而不是验证htp://fooxbvgf5766.herokuapp.com
。状态代码200会告诉我网址是一个真实的网站。
一直在搜索并找到this。
checkValidWebsite(appName) {
const url = `https://${appName}.herokuapp.com`
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}
运行该功能:
checkValidWebsite('foo')
.then(resp => {
console.log(resp)
})
.catch(e => {
console.log('Error:', e)
})
检查https://foo.herokuapp.com
我发现了错误。有没有办法检测到网址是一个真正的网站?