You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用jQuery实现按输入邮箱列表循环发送POST请求

实现方案

提供两种符合需求的实现方式,可根据后端接口支持的格式选择:


方案1:每个邮箱单独发送POST请求

先补充邮箱输入框的HTML结构,再修改JS逻辑实现循环发送:

<!-- 新增邮箱输入框 -->
<input type="text" id="emailInput" placeholder="请输入邮箱,多个用分号分隔" />
<button id="sendwebhook">Send</button>

<script>
$(document).ready(function() {
  $('#sendwebhook').click(function() {
    // 获取输入的邮箱内容,按分号分割、去除空格、过滤空值
    const inputVal = $('#emailInput').val().trim();
    const emailList = inputVal.split(';')
      .map(email => email.trim())
      .filter(email => email.length > 0);
    
    if (emailList.length === 0) {
      alert('请输入至少一个邮箱');
      return;
    }

    let successCount = 0;
    // 循环每个邮箱发送请求
    emailList.forEach(email => {
      $.ajax({
        url: "https://somesite.com/...",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ "email": email }),
        complete: function() {
          successCount++;
          // 所有请求完成后提示
          if (successCount === emailList.length) {
            alert(`全部请求处理完成,共发送${emailList.length}个请求`);
          }
        }
      });
    });
  });
});
</script>

方案2:合并所有邮箱单次发送请求

如果后端接口支持接收邮箱数组,可选择该方案减少请求次数,性能更优:

<input type="text" id="emailInput" placeholder="请输入邮箱,多个用分号分隔" />
<button id="sendwebhook">Send</button>

<script>
$(document).ready(function() {
  $('#sendwebhook').click(function() {
    const inputVal = $('#emailInput').val().trim();
    const emailList = inputVal.split(';')
      .map(email => email.trim())
      .filter(email => email.length > 0);
    
    if (emailList.length === 0) {
      alert('请输入至少一个邮箱');
      return;
    }

    $.ajax({
      url: "https://somesite.com/...",
      type: "POST",
      contentType: "application/json; charset=utf-8",
      // 组装为邮箱数组格式,可根据后端要求调整字段名
      data: JSON.stringify({ "emails": emailList }),
      complete: function() {
        alert(`请求发送成功,共提交${emailList.length}个邮箱`);
      }
    });
  });
});
</script>

可选优化点

  • 可增加简单的邮箱格式正则校验,过滤不符合规则的邮箱再发送请求
  • 可增加错误捕获逻辑,针对发送失败的邮箱做单独提示或重试处理

内容的提问来源于stack exchange,提问作者Niels Christian Andersen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 12:24:02