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

问题描述 投票:0回答:5

这是我的代码:

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...");}).success 不是函数

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

javascript jquery ajax post
5个回答
6
投票

你的语法有问题。您尝试做的是调用 $.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
投票

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


1
投票

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

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


0
投票

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

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


0
投票

参考: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" );
  });
© www.soinside.com 2019 - 2024. All rights reserved.