Jquery Ajax发布成功/错误功能

时间:2011-09-06 02:55:18

标签: jquery ajax

我正在尝试实现以下代码,这些代码成功运行,但是直接通过成功函数。我在那里放了一个警报,只显示一下,但是没有出现以下jquery消息调用。 (消息功能在同一页面的其他实例中有效)。

使用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 );    
        }
     })
    });
});

2 个答案:

答案 0 :(得分: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({

...  
     });

   });
});

答案 1 :(得分:0)

除非我误解了,否则你没有得到你的消息来显示文字?

I made a jsfiddle of the problem它似乎工作正常。

相关问题