2011-05-12 29 views
3

這裏是我的代碼:

var jqxhr = $.post("mypage.php", {url:pageurl}, function() { 
     alert("fetching..."); 
}) 
.success(function() { alert("fetch complete!"); }) 
.error(function() { alert("error!"); }) 
.complete(function() { alert("complete"); }); 

// Set another completion function for the request above 
jqxhr.complete(function(){ alert("second complete"); }); 

我得到的警報(‘正在獲取...’)對話框,但對其餘代碼沒有完成。我得到錯誤:$ .post(「sec_fetch_report.php」,function(){alert(「fetching ...」);})。成功不是函數

我想也許我可能會失蹤jquery庫,但我有另一個函數調用$ .post,它運行得很好。有什麼語法錯誤我在某處丟失或什麼? 謝謝

回答

7

您的語法已損壞。你試圖做的是調用$ .post()方法的.success屬性,這顯然不存在。看起來你還需要使用$.ajax方法而不是$.post

$.ajax({ 
    type: 'POST', 
    url: 'mypage.php', 
    data: { url: pageurl }, 
    beforeSend: function() 
    { 
     alert('Fetching....'); 
    }, 
    success: function() 
    { 
     alert('Fetch Complete'); 
    }, 
    error: function() 
    { 
     alert('Error'); 
    }, 
    complete: function() 
    { 
     alert('Complete') 
    } 
}); 
+2

他的語法在版本1.6中有效。 – ehynds 2011-05-12 18:08:58

+0

是的,它的有效。 $ .post()相當於$ .ajax({type:'POST'.... – 2011-05-12 18:10:26

+0

抱歉,必須downvote,這種語法是完全有效的.jqxhr對象是可鏈接的,從v1.5我相信 – 2011-05-12 18:10:40

2

這句法jQuery中唯一支持1.5+(引進deferreds的)。這聽起來像你正在使用jQuery的早期版本。如果您無法升級,請將成功/錯誤/完整處理程序作爲選項對象的方法(如Tejs的示例)。

+0

延遲是在jQuery 1.5中引入的http://api.jquery.com/category/deferred-object/ – Zirak 2011-05-12 18:15:37

+0

是的,謝謝你的糾正。 – ehynds 2011-05-12 18:18:43

0

見的例子在這裏:jQuery.post

也,你可能會得到錯誤一樣,如果你忘了鏈接jQuery庫

相關問題