weixin_33721427 2019-10-25 11:15 采纳率: 0%
浏览 96

通过Ajax将数据发送到API

I have simple form for sending email dato to API, but i want send data via Ajax for not reaload page and give success message under input tag.

HTML Sending form

HTML:

<form action="" method="POST" class="send-modal-data">
    <input type="text" id="send_email" name="subscribe-email" class="modal-input" placeholder="Email *">
    <button name="subscribe-form" class="danger-btn send-subscribe">Send</button>           
</form>

Ajax

  $(function() {
    $(".send-subscribe").click(function(e) {
    e.preventDefault();
    var settings = {
       email:  $("#send_email"),
      "url": "xxxx/api/user/trial/subscribe?email=" + email,
      "method": "POST",
      "timeout": 0,
    };

    $.ajax(settings).done(function (response) {
      console.log(response);
    });

    });
});

But when i send email, my modal window closing and not send data, how i can realize with right way ?

  • 写回答

1条回答 默认 最新

  • weixin_33747129 2019-10-25 11:29
    关注

    Your post function should look something like this:

    $(".send-subscribe").click(function() {
        $.post("https://xxx/api/user/subscribe", {
            email : $("#send_email")
        }, function(data, status){
            console.log(status + " :: " + data);
        });
    }); 
    

    You than can use status and data to continue your work.

    评论

报告相同问题?

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真