AJAX: Submitting a form without refreshing the page

前端 未结 8 2011
感动是毒
感动是毒 2020-11-30 11:43

I have a form similar to the following:

8条回答
  •  野性不改
    2020-11-30 12:28

    You need to prevent default action if you are using input type as submit .

    By putting $("form").submit(...) you're attaching the submit handler, this will submit form (this is default action).

    If don't want this default action use preventDefault() method.

    If you are using other than submit, no need to prevent default.

     $("form").submit(function(e) {
        e.preventDefault();
        $.ajax({
          type: 'POST',
          url: 'save.asmx/saveData',
          dataType: 'json',
          contentType:"application/json;charset=utf-8",
          data: $('form').serialize(),
          async:false,
          success: function() {
            alert("success");
          }
          error: function(request,error) {
            console.log("error");
          }
    

提交回复
热议问题