Is there a vanilla javascript alternative available for the following code snippet?
function check() {
var restURL = "https://apilayer.net/api/check?access_key=c5118f1f9827f42a5fc4b231932130a8&email=" + document.getElementById('email').value + "&smtp=1&format=1"
$.ajax({
type: 'GET',
url: restURL,
dataType: "json",
success: renderList,
});
return false;
}
function renderList(data) {
if ((data.format_valid == true) && (data.smtp_check == true)) {
alert("Valid email");
}
else {
alert("Invalid email");
}
}
This is the only place where I'm using jQuery and using the whole jQuery library for this does not sound like a good idea. I have tested the script for email verification and it works perfectly.
When I was finding the VanillaJS alternatives of jQuery Ajax, I came across http://youmightnotneedjquery.com/ and this is the code I could write using that website but it does not show any output at all:
function check() {
var restURL = "https://apilayer.net/api/check?access_key=c5118f1f9827f42a5fc4b231932130a8&email=" + document.getElementById('email').value + "&smtp=1&format=1"
var request = new XMLHttpRequest();
request.open('GET', restURL, true);
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
//SUCCESS
var resp = this.response;
renderList(resp.data);
} else {
// We reached our target server, but it returned an error
alert("Server returned an error");
}
};
request.onerror = function() {
alert("Connection Error");
// There was a connection error of some sort
};
request.send();
}
function renderList(data) {
if ((data.format_valid == true) && (data.smtp_check == true)) {
alert("Valid email");
} else {
alert("Invalid email");
}
}
<input type="email" id="email" value="x@.com" />
<button onclick="check()"> Click me</button>