【问题标题】:why doesn't this $.post() work on iOS?为什么这个 $.post() 在 iOS 上不起作用?
【发布时间】:2018-05-23 01:01:35
【问题描述】:

为什么以下表单在 iOS 上不起作用?

它适用于 Chrome / Android / Windows / Mac,但不适用于 iOS 上的 Safari / Chrome。我已将问题范围缩小到 jquery post 函数。

它通过了“e.preventDefault()”但从不发布数据(我有一个 php 脚本确认该帖子永远不会发送到服务器)。并且带有 alert() 的回调不会触发。

<form method="post" action="" id="wl_form">
<div class="form-group">
    <label for="first">First Name *</label>
    <input type="text" name="first" class="form-control" id="first">
</div>
<button name="submit" type="submit" id="submit" class="btn btn-default">Submit</button>
</form>

var form = document.getElementsByTagName('form')[0];

form.addEventListener('submit', doSubmit);

function doSubmit (e) {

    e.preventDefault();

    alert("ive made it this far");

    $.post("record.php",
    {
        first: $("#first").val()
    },
    function(data){
        console.log("submitting");
        alert("Success: " + data);
    });

    window.location.href = "nextpage.html";
}

【问题讨论】:

  • 如果注释掉 window.location.href 行会怎样?
  • 啊!而已。我将href移到回调中,现在一切都很好。谢谢!将其作为一个答案将其标记为正确。

标签: javascript jquery ios forms submit


【解决方案1】:

看起来您在发布 ajax 调用后立即执行重定向。如果您执行页面传输的速度足够快,则 ajax 请求将被取消,您的后端可能永远看不到它。

正如您从 cmets 中发现的,将重定向移动到成功回调中应该允许您完全执行 ajax 请求,然后执行重定向。

【讨论】:

  • 每次我用“看起来像”开始回答这些问题时,我都觉得自己更像 Clippy。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 2022-08-02
  • 2021-04-26
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多