【问题标题】:jquery ajax to vanilla javascript (normal javascript) code conversion requestjquery ajax 到 vanilla javascript(普通 javascript)代码转换请求
【发布时间】:2021-09-18 14:48:47
【问题描述】:

是否有可用于以下代码 sn-p 的 vanilla javascript 替代方案?

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");
    }
}

这是我使用 jQuery 的唯一地方,并且为此使用整个 jQuery 库听起来不是一个好主意。我已经测试了用于电子邮件验证的脚本,它运行良好。

当我发现 jQuery Ajax 的 VanillaJS 替代品时,我遇到了http://youmightnotneedjquery.com/,这是我可以使用该网站编写的代码,但它根本没有显示任何输出:

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>

【问题讨论】:

  • 我给你做了一个sn-p。你在某个地方给check打过电话吗?代码看起来正确并运行
  • 你可能想换行:+encodeURIComponent(document.getElementById('email').value )+...
  • @mplungjan 不,它不显示任何输出。我将编辑帖子并放置访问密钥,以便您查看
  • @mplungjan 我已经使用访问密钥更新了 jquery 和 vanillaJS 代码,并且还包含了 html,现在您可以运行它们并自行检查。
  • 对于未来的编辑,请直接编辑 sn-p。让您更轻松地查看代码的运行情况。

标签: javascript jquery ajax api email-validation


【解决方案1】:
  1. 使用 https
  2. 我们需要 JSON.parse renderList(JSON.parse(this.response));

请注意,在测试有效电子邮件之前,我已达到您的限制

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
      renderList(JSON.parse(this.response));
    } 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 or issue with the api");
  }
}
<input type="email" name="email" id="email">
<button onclick="check()"> Click me</button>

获取示例

function check() {
  var restURL = "https://apilayer.net/api/check?smtp=1&format=1&access_key=c5118f1f9827f42a5fc4b231932130a8&email=" + encodeURIComponent(document.getElementById('email').value)
  fetch(restURL)
    .then(response => response.json())
    .then(data => renderList(data));
}

function renderList(data) {
  if (data.email && data.smtp_check) {
    alert("Valid email");
  } else {
    if (data.error) {
      if (data.error.type.includes('limit')) console.log("drat, limit reached")
      else if (data.error.type.includes("no_email")) {
        alert("empty email")
      } else {
        alert("Invalid email");
        console.log(data)
      }
    } else console.log("unknown issue", data)
  }
}
<input type="email" name="email" id="email">
<button onclick="check()"> Click me</button>

【讨论】:

  • 最初的 jQuery 代码对我来说仍然可以正常工作。如果电子邮件有效,则警告“有效电子邮件”,如果输入的电子邮件无效,则警告“无效电子邮件”。所以,你还没有达到极限(否则它对我不起作用)。一定是另一个错误。
  • 你现在可以使用原来的渲染列表了。
  • 我也给了你一个 fetch 版本
  • 是的,他们现在正在工作。非常感谢。获取版本似乎更好。但是每当我输入无效邮件时,它都会说未知问题是控制台。我想我应该把它改成 alert("Invalid email") 因为它的 smtp_check 是假的
  • 是的,非常抱歉。我太兴奋了,终于能够完成这项工作,因为我忘了接受答案。我现在已经接受了你的回答。
猜你喜欢
  • 2011-07-24
  • 2021-09-25
  • 2018-06-03
  • 2022-12-07
  • 2011-06-10
  • 2012-05-19
  • 2015-01-25
  • 2020-01-18
  • 2021-02-11
相关资源
最近更新 更多