javascript $.post 失败并显示错误“...不是函数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5982300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:02:12  来源:igfitidea点击:

$.post fails with error "...is not a function"

javascriptjqueryajaxpost

提问by Lazloman

Here is my code:

这是我的代码:

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

I get the alert("Fetching...") dialog box, but the rest of the code does not complete. I get the error: Error: $.post("sec_fetch_report.php", function () {alert("fetching...");}).success is not a function

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

I thought maybe I might be missing the jquery libs, but I have another function that calls $.post and it runs just fine. Is there a syntax error I'm missing somewhere or what? Thanks

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

回答by Tejs

Your syntax is broken. What you're attempting to do is call the .success property of the $.post() method, which obviously doesnt exist. It looks like you also need to be using the $.ajaxmethod instead of $.post:

你的语法坏了。你试图做的是调用 $.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')
     }
 });

回答by ehynds

That syntax is only supported in jQuery 1.5+ (with the introduction of deferreds). It sounds like you're using a earlier version of jQuery. If you aren't able to upgrade, pass the success/error/complete handlers as methods of the options object (like in Tejs's example).

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

回答by Gary Green

The jqxhr object is chainable from v1.5. Make sure you have this version or later.

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

Ref: jQuery 1.5 released, now with Deferred Objects

参考:jQuery 1.5 发布,现在带有延迟对象

回答by Scherbius.com

See examples here: jQuery.post

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

also, you may get error like that if you've forgotten to link jQuery library

此外,如果您忘记链接 jQuery 库,您可能会收到类似的错误