$ .post失败,错误“...不是函数”

时间:2011-05-12 18:00:20

标签: javascript jquery ajax post

这是我的代码:

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的函数,它运行得很好。是否存在语法错误我在某处丢失或者什么? 感谢

4 个答案:

答案 0 :(得分: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')
     }
 });

答案 1 :(得分:2)

只有jQuery 1.5+支持该语法(引入deferreds)。听起来你正在使用早期版本的jQuery。如果您无法升级,请将success / error / complete处理程序作为options对象的方法传递(如Tejs的示例中所示)。

答案 2 :(得分:1)

jqxhr对象可以从v1.5链接。确保您拥有此版本或更高版本。

参考:jQuery 1.5 released, now with Deferred Objects

答案 3 :(得分:0)

请参阅此处的示例:jQuery.post

另外,如果您忘记链接jQuery库

,可能会出现类似的错误