2011-09-06 141 views
2

我试图实现下面的代码,它运行成功,但是通过成功函数正确地吹。我在那里只显示一个提示,但是下面的jquery消息调用没有出现。 (消息功能在其他情况下在同一页面上工作)。jquery Ajax post成功/错误函数

使用jquery 1.6。

任何想法为什么代码没有执行,因为它应该?谢谢!

$(document).ready(function(){ 
    $('#postride').submit(function() { 

    dataString = $("#postride").serialize(); 
    $.ajax({ 
     type: "post", 
     url: "postride.php", 
     data:dataString, 
     error: function() { 
      alert('error!'); 
      $(".message").text('An error occurred'); 
      $(".message").fadeIn("slow"); 
      $(".message").delay(2000).fadeOut(1000);  
     }, 
     success: function() { 
      alert('success!'); 
      $(".message").text('Your post has been completed'); 
      $(".message").fadeIn("slow"); 
      $(".message").delay(2000).fadeOut(1000);  
      setTimeout(function() { top.location.href="view.php" }, 3000);  
     } 
    }) 
    }); 
}); 

回答

4

我觉得#postride是您正在使用ajax发布的形式。你的代码中的问题是你没有停止提交表单的默认行为。只要您提交表单是在ajax呼叫响应来临之前发布的,您几乎看不到任何东西。

试试这个。

$(document).ready(function(){ 
    $('#postride').submit(function(e) { 
    //This line will prevent the form from submitting 
    e.preventDefault(); 


    dataString = $("#postride").serialize(); 
    $.ajax({ 

... 
    }); 

    }); 
}); 
+0

完美的工作。谢谢!! – Matt