$.post 失败并出现错误“...不是函数”

发布于 2024-11-07 05:00:21 字数 591 浏览 1 评论 0原文

这是我的代码:

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

我收到警报(“正在获取...”)对话框,但其余代码未完成。我收到错误: Error: $.post("sec_fetch_report.php", function () {alert("fetching...");}).success 不是一个函数

我想我可能会丢失 jquery 库,但我有另一个调用 $.post 的函数,它运行得很好。我在某处遗漏了语法错误还是什么? 谢谢

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦回梦里 2024-11-14 05:00:21

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

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 $.ajax method instead of $.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')
     }
 });
情话难免假 2024-11-14 05:00:21

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

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).

〃安静 2024-11-14 05:00:21

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

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

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

Ref: jQuery 1.5 released, now with Deferred Objects

赏烟花じ飞满天 2024-11-14 05:00:21

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

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

See examples here: jQuery.post

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

玩物 2024-11-14 05:00:21

参考: https://api.jquery.com/jQuery.post/

从 jQuery 1.5 开始,所有 jQuery 的 Ajax 方法都返回 XMLHTTPRequest 对象的超集。这个 jQuery XHR 对象,或“jqXHR”
$.post() 返回实现了 Promise 接口,提供了一切
Promise 的属性、方法和行为......

Promise 接口还允许 jQuery 的 Ajax 方法,包括
$.get(),链接多个 .done().fail().always() 回调
在单个请求上,甚至在
请求可能已完成。如果请求已经完成,则
回调立即被触发。

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });

Ref: https://api.jquery.com/jQuery.post/

As of jQuery 1.5, all of jQuery's Ajax methods return a superset of the XMLHTTPRequest object. This jQuery XHR object, or "jqXHR,"
returned by $.post() implements the Promise interface, giving it all
the properties, methods, and behavior of a Promise ......

The Promise interface also allows jQuery's Ajax methods, including
$.get(), to chain multiple .done(), .fail(), and .always() callbacks
on a single request, and even to assign these callbacks after the
request may have completed. If the request is already complete, the
callback is fired immediately.

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文