【问题标题】:Detect $.post() faliure [duplicate]检测 $.post() 失败 [重复]
【发布时间】:2016-09-09 19:30:07
【问题描述】:

当页面加载时,我设置了一个 jquery $.post()

$.post("area.php", {'user': $('#state').val()},
function(info){
      //do something
});

所以在加载时会调用 php,有没有办法知道是否由于连接失败或类似情况而没有收到 info

【问题讨论】:

  • 作为建议,考虑使用$('#the-form').serialize() 而不是手动构建 POST 数据。

标签: javascript jquery


【解决方案1】:

是的。您只需在已有的功能上添加第二个功能或使用类似的承诺。

$.post( "example.php", function() {
  alert( "success" );
})
.done(function() {
  alert( "second success" );
})
.fail(function() {
  alert( "error" );
})
.always(function() {
  alert( "finished" );
});

发件人:https://api.jquery.com/jquery.post/

【讨论】:

  • $.post 只是 $.ajax 的快捷方式,它为您提供 type="POST" 部分。 $.ajax 还公开了整个方法,而 $.post 可能不包括所有内容。
【解决方案2】:

试试这个:

$.post("area.php", { 'user': $('#state').val() }).done(function () {
    console.log("success");
})
.fail(function () {
    console.log("error");
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-29
    • 2011-02-08
    • 2014-09-16
    • 2023-04-01
    • 2019-07-29
    • 1970-01-01
    • 2019-10-25
    • 2017-03-23
    相关资源
    最近更新 更多