How to send form data without page refresh?

前端 未结 4 709
清歌不尽
清歌不尽 2021-01-06 03:52

I have sending my data in ajax using submit button without any page refresh. But the page refreshed.

Please check my code and let me know the problem.



        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 04:37

    use event.preventdefault()

    $("#idForm").submit(function(e) { 
    e.preventDefault();//use it here to stop default behaviour
       alert("hi"); 
       var url = "ajax.php";
        $.ajax({
               type: "POST",
               url: url,
               data: $("#idForm").serialize(),
               success: function(data) {
                   alert(data);
               }
             });
    
        return false;
    });
    

提交回复
热议问题