2012-04-25 169 views
-4

我想将我的$.post调用转换为$.ajax调用,因为我需要清理每个调用的缓存。

$.post电话如下:

 $.post("test", 
      function(data) { 
       $("#test").html(data); 
       initTest(); 
      } 
     ).success(function(){ 
      $('.box1').hide("slide", {direction: "left"}, 1000); 
      $('.box3').show("slide", {direction: "right"}, 1000); 
     }); 

我试过,但它不工作...

$.ajax({ 
      type: "POST", 
      url: "test", 
      success: function (data) { 
       $("#test").html(data); 
       $('.box1').hide("slide", {direction: "left"}, 1000); 
       $('.box3').show("slide", {direction: "right"}, 1000); 
      }, 
      dataType: "json", 
      cache: false 
    }); 
+2

您是否打算阅读[documentation](http://api.jquery.com/jQuery.post/)?这是它提到的第一件事。 – Jon 2012-04-25 11:20:06

+1

是的..当然..但它不起作用 – newbie 2012-04-25 11:23:21

+0

什么不工作?也许是因为url =“test”? – 2012-04-25 11:23:45

回答

2
$.ajax({ 
    method:"POST", 
    url : "test.php", 
    success : function(data) { 
       $("#test").html(data); 
       initTest(); 
       $('.box1').hide("slide", {direction: "left"}, 1000); 
    $('.box3').show("slide", {direction: "right"}, 1000); 
      }, 
    cache : false 
}); 

UPDATE 我认为你有问题,在分析数据。 $.post默认为html dataType。在$.ajax调用你改变它的“json”。如果没有json响应,所以你有解析错误和成功处理程序不打电话。

+0

tnx ..我刚刚删除了数据类型,它的工作原理! – newbie 2012-04-25 11:34:34

1
$.ajax('test') 
    .done(function() { 
     $('.box1').hide('slide', {direction: 'left'}, 1000); 
     $('.box3').show('slide', {direction: 'right'}, 1000); 
    }) 
    .fail(function() { alert('error'); }) 
    .always(function() { alert('complete'); });