【问题标题】:$.post fails with error "...is not a function"$.post 失败并出现错误“...不是函数”
【发布时间】:2011-08-24 08:22:18
【问题描述】:

这是我的代码:

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"); });

我收到警报(“Fetching...”)对话框,但其余代码未完成。我收到错误:错误:$.post("sec_fetch_report.php", function () {alert("fetching...");}).success is not a function

我想也许我可能缺少 jquery 库,但我有另一个调用 $.post 的函数,它运行得很好。是否有我在某处遗漏的语法错误或什么? 谢谢

【问题讨论】:

    标签: javascript jquery ajax post


    【解决方案1】:

    在此处查看示例:jQuery.post

    另外,如果你忘记链接 jQuery 库,你可能会得到类似的错误

    【讨论】:

      【解决方案2】:

      该语法仅在 jQuery 1.5+ 中受支持(引入了延迟)。听起来您使用的是早期版本的 jQuery。如果您无法升级,请将成功/错误/完成处理程序作为选项对象的方法传递(如 Tejs 的示例中)。

      【讨论】:

      【解决方案3】:

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

      参考:jQuery 1.5 released, now with Deferred Objects

      【讨论】:

        【解决方案4】:

        你的语法有问题。您尝试做的是调用 $.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.6版本有效。
        • 抱歉不得不投反对票,这种语法是完全有效的。 jqxhr 对象是可链接的,我相信从 v1.5 开始。
        • +1 因为此语法在某些(相对较新的)jQuery 版本中无效,帖子中未指定。但是,为了准确性/完整性,此回复应更新以包含需要上述方法的 jQuery 版本(或哪个 jQuery 版本首先允许建议的可链接方法)。
        猜你喜欢
        • 1970-01-01
        • 2014-04-10
        • 1970-01-01
        • 2014-08-02
        • 1970-01-01
        • 2021-10-16
        • 2015-04-13
        • 1970-01-01
        • 2018-02-24
        相关资源
        最近更新 更多