Using Sinatra and jQuery without redirecting on POST

≡放荡痞女 提交于 2019-12-04 19:42:20

Try this,

$(function() {
  $("form#the_id").submit(function(e){
    e.preventDefault();

    $.ajax({
      type: "POST",
      url: "/register",
      data: $('form.register').serialize(),
      success: function(){
        $("#message").html("Successfully registered")
      },
      error: function(){
        $("#message").html("Not Successful")
      }
    });
  });
});

$("form#the_id").submit(... detects the form submission. e.preventDefault(); prevents the form submission. The rest submits an Ajax request.

Don't forget to change #the_id to your form id.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!