ajax请求不会阻止页面刷新

时间:2014-01-14 02:28:43

标签: php jquery ajax form-submit page-refresh

这个ajax请求有什么问题?页面仍在重新加载而不提供任何弹出窗口。如果我删除“event.preventDefault();”之后的所有内容它将停止页面重新加载所以我认为它必须与我如何使用ajax方法。这是一个php-self验证表单

<script type="text/javascript">
             //attach submit event handler to the form
       $('#rsgform1').submit(function(event) {

           //prevent the form from submitting by default
           event.preventDefault();

         //Clear result div
       $("#rsgresult").html('');

         //get values from from
          var values = $(this).serialize();

           // do an ajax request   
         $.ajax({
             url: "contact.php",  
            type: "post",
            data: values,
            success: function(){
              alert("success");
              $("#rsgresult").html('Submitted successfully');
            },
            error:function(){
               alert("failure");
             $("#rsgresult").html('There is error while submit');
            }

        });
         </script>

4 个答案:

答案 0 :(得分:2)

你忘了关闭ajax电话......

<script type="text/javascript">
             //attach submit event handler to the form
       $('#rsgform1').submit(function(event) {

           //prevent the form from submitting by default
           event.preventDefault();

         //Clear result div
       $("#rsgresult").html('');

         //get values from from
          var values = $(this).serialize();

           // do an ajax request   
         $.ajax({
             url: "contact.php",  
            type: "post",
            data: values,
            success: function(){
              alert("success");
              $("#rsgresult").html('Submitted successfully');
            },
            error:function(){
               alert("failure");
             $("#rsgresult").html('There is error while submit');
            }
         });

       });
     </script>

答案 1 :(得分:2)

在回调函数结束时尝试return false

别忘了像其他人提到的那样平衡你的牙套。

答案 2 :(得分:1)

您的脚本末尾有一对错过的右括号})

$('#rsgform1').submit(function (event) {

    //prevent the form from submitting by default
    event.preventDefault();

    //Clear result div
    $("#rsgresult").html('');

    //get values from from
    var values = $(this).serialize();

    // do an ajax request   
    $.ajax({
        url: "contact.php",
        type: "post",
        data: values,
        success: function () {
            alert("success");
            $("#rsgresult").html('Submitted successfully');
        },
        error: function () {
            alert("failure");
            $("#rsgresult").html('There is error while submit');
        }

    });
});// <-- missing this closing pair

答案 3 :(得分:1)

您的JS中有错误。发生错误时,页面会刷新。

<script type="text/javascript">
   $('#rsgform1').submit(function(event) {
       event.preventDefault();

       $("#rsgresult").html('');
       var values = $(this).serialize();

       $.ajax({
         url: "contact.php",  
         type: "post",
         data: values,
         success: function() {
           alert("success");
           $("#rsgresult").html('Submitted successfully');
         },
         error:function() {
           alert("failure");
           $("#rsgresult").html('There is error while submit');
         }
       }); // Missing
   });
</script>