从 iOS 提交时,AJAX 不会触发 PHP 邮件程序

问题描述 投票:0回答:1
dataString = 'param=stuff';
jQuery.ajax({
   cache: false,
   type: "POST",
   headers: { "cache-control": "no-cache" },
   url: "https://example.com/assets/mailer.php",
   data: dataString, 
   success: window.location.href = "https://example.com/thankyou"
});

这适用于 Windows、MacOS 和 Android 上的各种浏览器。在 iOS 上,不会触发 PHP 邮件程序脚本,但重定向有效。使用 Safari 开发工具,我可以看到控制台中没有任何错误。我错过了什么?

php jquery ajax mailer
1个回答
0
投票

success
接受一个函数,您正在向它传递一个字符串,同时将该字符串分配给
window.location.href
并导航离开。

改用函数...

jQuery
  .post("https://example.com/assets/mailer.php", { param: "stuff" })
  .done(() => {
    window.location.href = "https://example.com/thankyou";
  });

请注意,我已经删除了您设置的所有冗余选项,并使用了更安全的编码请求正文的方法。


仅供参考,您绝对为此不需要 jQuery

fetch("https://example.com/assets/mailer.php", {
  method: "POST",
  body: new URLSearchParams({ param: "stuff" }),
}).then((res) => {
  if (res.ok) {
    window.location.href = "https://example.com/thankyou";
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.